2013-10-02 116 views
0

我使用實體框架5代碼優先,並且存在組合鍵的一些問題。實體框架中的表處理中的複合鍵

我有這些表

enter image description here

enter image description here

這是怎麼我的實體

public class Product : EntityBase 
    { 
     public Product() 
     { 
      this.ProductArticles = new List<ProductArticle>(); 
     } 

     [Key, Column(Order = 0)] 
     [DatabaseGenerated(DatabaseGeneratedOption.None)] 
     public int ProductId { get; set; } 
     [Key, Column(Order = 1)] 
     public int PricelistId { get; set; } 
     public string Description { get; set; } 
     public string Unit { get; set; } 
     public string ReportText1 { get; set; } 
     public string ReportText2 { get; set; } 
     public bool Standard { get; set; } 
     public int ProductGroupId { get; set; } 
     public decimal? Surcharge1 { get; set; } 
     public decimal? Surcharge2 { get; set; } 
     public decimal? Surcharge3 { get; set; } 
     public decimal? Surcharge4 { get; set; } 
     public decimal PriceMaterialIn { get; set; } 
     public decimal AdjMaterialIn { get; set; } 
     public decimal F_PriceMaterialInAdj { get; set; } 
     public decimal F_AdjMaterial { get; set; } 
     public decimal F_PriceMaterialOut { get; set; } 
     public decimal PriceArtisanIn { get; set; } 
     public decimal F_AdjArtisan { get; set; } 
     public decimal F_PriceArtisanOut { get; set; } 
     public decimal F_TotalOut { get; set; } 
     public decimal F_TotalOutVat { get; set; } 
     public bool GetPrice { get; set; } 
     public string Notes { get; set; } 
     [ForeignKey("ProductGroupId,PricelistId")] 
     public virtual ProductGroup ProductGroup { get; set; } 
     [ForeignKey("ProductId,PricelistId")] 
     public virtual ICollection<ProductArticle> ProductArticles { get; set; } 
     [ForeignKey("PricelistId")] 
     public virtual Pricelist Pricelist { get; set; } 
    } 



public class ProductGroup : EntityBase 
    { 
     [Key, Column(Order = 0)] 
     public int ProductGroupId { get; set; } 
     [Key, Column(Order = 1)] 
     public int PricelistId { get; set; } 
     public int OptionalGroupId { get; set; } 
     public string Prefix { get; set; } 
     public string Description { get; set; } 
     public decimal? Surcharge1 { get; set; } 
     public decimal? Surcharge2 { get; set; } 
     public decimal? Surcharge3 { get; set; } 
     public decimal? Surcharge4 { get; set; } 
     public string ReportText1 { get; set; } 
     public string ReportText2 { get; set; } 
     [ForeignKey("OptionalGroupId,PricelistId")] 
     public virtual OptionalGroup OptionalGroup { get; set; } 
     [ForeignKey("PricelistId")] 
     public virtual Pricelist Pricelist { get; set; } 
    } 

但地圖時的背景是建立我得到這個消息

337,10):錯誤3015:映射從第 行開始的映射片段中的問題303,337:來自表 的外鍵約束'Product_ProductGroup'ProductGroup (ProductGroupId,PricelistId)::產品(PricelistId,ProductGroupId) :在概念方面,外鍵必須 映射到某個參與 外鍵關聯的AssociationSet或EntitySets。

+0

對於關鍵ProductGroupId,PricelistId引用產品表上的任何記錄,產品表需要同時具有ProductGroupId和PricelistId等效列。我可以看到PricelistId列 - 但產品表中ProductGroupId的等價物是什麼? –

+0

我在產品表中參考了productgroupid .. –

回答

0

我通過重新設計表格解決了這個問題。認爲組合鍵是糟糕的設計。