Composite key in table manupulation in Entity Framework

I use Entity Framework 5 Code First and have some problem with composite keys.

I have those tables

And this is how i map the entities

 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; }
    }

But when the context is build i get this message

337,10) : error 3015: Problem in mapping fragments starting at lines 303, 337:Foreign key constraint 'Product_ProductGroup' from table Product (PricelistId, ProductGroupId) to table ProductGroup (ProductGroupId, PricelistId):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.


I solved it by redesign the table. Think that composite key are bad design.

链接地址: http://www.djcxy.com/p/37478.html

上一篇: 如何在实体框架中添加外键关系?

下一篇: 实体框架中表格处理中的复合键