实体框架中表格处理中的复合键

我使用实体框架5代码优先和组合键有一些问题。

我有那些桌子

这就是我如何映射实体

 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)到Table ProductGroup(ProductGroupId,PricelistId)的外键约束'Product_ProductGroup :: ::映射不足::外键必须是映射到在概念方面参与外键关联的一些AssociationSet或EntitySets。


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

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

上一篇: Composite key in table manupulation in Entity Framework

下一篇: One to one optional relationship using Entity Framework Fluent API