【问题标题】:Composite key in table manupulation in Entity Framework实体框架中表操作中的复合键
【发布时间】:2026-01-04 10:10:02
【问题描述】:

我使用 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_ProductGroup” Product (PricelistId, ProductGroupId) 到表 ProductGroup (ProductGroupId, PricelistId):: 映射不足:外键必须 映射到一些 AssociationSet 或 EntitySets 参与 概念方面的外键关联。

【问题讨论】:

  • 对于键 ProductGroupId、PricelistId 来引用产品表上的任何记录,产品表需要同时具有 ProductGroupId 和 PricelistId 等效列。我可以看到 PricelistId 列 - 但是产品表中 ProductGroupId 的等价物是什么?
  • 我 productgroupid 在产品表中被引用..

标签: entity-framework composite-key


【解决方案1】:

我通过重新设计表格解决了这个问题。认为复合键是糟糕的设计。

【讨论】: