我使用 Entity Framework 5 Code First 并且对复合键有一些问题。
我有那些桌子
这就是我映射实体的方式
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(PricelistId、ProductGroupId)到表 ProductGroup(ProductGroupId、PricelistId)的外键约束“Product_ProductGroup”::映射不足:外键必须是映射到在概念方面参与外键关联的一些 AssociationSet 或 EntitySet。