Entity framework 实体框架中表操作中的复合键
我首先使用EntityFramework5代码,但复合键有一些问题 我有那些桌子 这就是我映射实体的方式Entity framework 实体框架中表操作中的复合键,entity-framework,composite-key,Entity Framework,Composite Key,我首先使用EntityFramework5代码,但复合键有一些问题 我有那些桌子 这就是我映射实体的方式 public class Product : EntityBase { public Product() { this.ProductArticles = new List<ProductArticle>(); } [Key, Column(Order = 0)]
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; }
}
公共类产品:EntityBase
{
公共产品()
{
this.ProductArticles=新列表();
}
[键,列(顺序=0)]
[数据库生成(DatabaseGeneratedOption.None)]
public int ProductId{get;set;}
[键,列(顺序=1)]
public int PricelistId{get;set;}
公共字符串说明{get;set;}
公共字符串单元{get;set;}
公共字符串ReportText1{get;set;}
公共字符串ReportText2{get;set;}
公共布尔标准{get;set;}
public int ProductGroupId{get;set;}
公共十进制?附加费1{get;set;}
公共十进制?附加费2{get;set;}
公共十进制?附加费3{get;set;}
公共十进制?附加费4{get;set;}
公共十进制价格{get;set;}
公共十进制AdjMaterialIn{get;set;}
公共十进制F_PriceMaterialInAdj{get;set;}
公共十进制F_AdjMaterial{get;set;}
公共十进制F_PriceMaterialOut{get;set;}
在{get;set;}中的公共十进制值
公共十进制F_AdjArtisan{get;set;}
公共十进制F_{get;set;}
公共十进制F_TotalOut{get;set;}
公共十进制F_TotalOutVat{get;set;}
公共bool GetPrice{get;set;}
公共字符串注释{get;set;}
[ForeignKey(“ProductGroupId,PricelistId”)]
公共虚拟产品组产品组{get;set;}
[ForeignKey(“ProductId,PricelistId”)]
公共虚拟ICollection ProductArticles{get;set;}
[ForeignKey(“PricelistId”)]
公共虚拟价格表价格表{get;set;}
}
公共类ProductGroup:EntityBase
{
[键,列(顺序=0)]
public int ProductGroupId{get;set;}
[键,列(顺序=1)]
public int PricelistId{get;set;}
public int OptionalGroupId{get;set;}
公共字符串前缀{get;set;}
公共字符串说明{get;set;}
公共十进制?附加费1{get;set;}
公共十进制?附加费2{get;set;}
公共十进制?附加费3{get;set;}
公共十进制?附加费4{get;set;}
公共字符串ReportText1{get;set;}
公共字符串ReportText2{get;set;}
[ForeignKey(“OptionalGroupId,PricelistId”)]
公共虚拟OptionalGroup OptionalGroup{get;set;}
[ForeignKey(“PricelistId”)]
公共虚拟价格表价格表{get;set;}
}
但在构建上下文时,我得到了这个消息
337,10):错误3015:映射从行开始的片段时出现问题
303337:表中的外键约束“Product\u ProductGroup”
产品(PricelistId、ProductGroupId)到表ProductGroup
(ProductGroupId,PricelistId)::映射不足:必须使用外键
映射到参与
概念方面的外键关联
我通过重新设计桌子解决了这个问题。认为复合键是糟糕的设计。对于键ProductGroupId,PricelistId要引用产品表上的任何记录,产品表需要同时具有ProductGroupId和PricelistId等效列。我可以看到PricelistId列,但是ProductGroupId在product表中的等价物是什么?ProductGroupId在product表中引用。。