![](/img/trans.png)
[英]Create a table in SQL where the Composite Primary Key is also a Foreign Key
[英]Design DB where primary key of table is also foreign key of another table
我試圖找出設計我的數據庫的最佳方式。 我有 2 個表, Product
和Image
。
產品
PId PRIMARY KEY IDENTITY(1, 1)
PName
PPrice
圖片
PId PRIMARY KEY IDENTITY(1,1)
PImage1
PImage2
FOREIGN KEY (PId) REFERENCES Product(PId)
目前我的數據庫設計得很糟糕。 因為我假設每當添加Product
條目時,就在添加Image
條目之后。 此假設還假設為Image
生成的主鍵與為Product
生成的主鍵相同。
然后在控制器代碼中我有:
_context.Product.Add(Product); // Add the created product record to the database
await _context.SaveChangesAsync();
_context.Image.Add(imageRecord); // Add the created image record to the database.
await _context.SaveChangesAsync();
其中Product
和imageRecord
分別是以下模型類的對象:
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public string ProductShortDesc { get; set; }
public string ProductLongDesc { get; set; }
public decimal ProductPrice { get; set; }
}
public class Image
{
[ForeignKey("Product")]
[Key]
public int ProductID { get; set; }
public byte[] ProductImage1 { get; set; }
public byte[] ProductImage2 { get; set; }
public byte[] ProductImage3 { get; set; }
}
當我開始從表中刪除條目時,這開始失敗,並且自動遞增的主鍵值不再相同。
我怎樣才能更好地設計它,以便我能夠選擇Product
的主鍵,然后將其用作Image
的主鍵?
下面是一個演示,其中Product
和Image
具有一對一的關系。
Image
已將ProductID
設置為 PK 和 FK。
然后在Image.ProductID
上添加[DatabaseGenerated(DatabaseGeneratedOption.None)]
屬性來手動設置 PK 的值,而不是自動遞增。
public class Product
{
[Key]
public int ProductID { get; set; }
public string ProductName { get; set; }
public string ProductShortDesc { get; set; }
public string ProductLongDesc { get; set; }
public decimal ProductPrice { get; set; }
public Image Image { get; set; }
}
public class Image
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ProductID { get; set; }
public byte[] ProductImage1 { get; set; }
public byte[] ProductImage2 { get; set; }
public byte[] ProductImage3 { get; set; }
[ForeignKey("ProductID")]
public Product Product { get; set; }
}
控制器:
_context.Products.Add(Product);
await _context.SaveChangesAsync();
var imageRecord = new Image()
{
ProductID = Product.ProductID,
ProductImage1 = xxx,
//other properties
};
_context.Images.Add(imageRecord); // Add the created image record to the database.
await _context.SaveChangesAsync();
最后,您將創建兩個具有相同 PK 值的實體並使用級聯刪除。
在 Image 表中,我會讓它擁有自己的數據庫生成的主鍵,並將 ProductId 僅作為外鍵。
如果要強制執行一對一,可以通過圖像表中 ProductId 列的唯一約束來實現。
更重要的是,您還可以住一張桌子。 創建兩個表的原因是具有1到N的關系。 桌子的寬度也非常小。
我建議將圖片存儲在服務器文件系統中。 我還建議創建一個帶有圖片的表格。
public class Product
{
[Key]
public int ProductID { get; set; }
public string ProductName { get; set; }
public string ProductShortDesc { get; set; }
public string ProductLongDesc { get; set; }
public decimal ProductPrice { get; set; }
public Icollection<Image> Images { get; set; }
}
public class Image
{
[Key]
public int ImageId { get; set; }
public int ProductId { get; set; }
public stirng ImageName { get; set; }
[ForeignKey("ProductID")]
public Product Product { get; set; }
}
現在獲取圖片,滿足從 Images 表中獲取 PruductId 的請求就足夠了。
將來,這將允許您輕松更改圖像數量。 它還將加快您的應用程序的工作,因為您不必解析圖片。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.