首页 文章

在实体框架中的表操作中的复合键

提问于
浏览
0

我使用Entity Framework 5 Code First并且对复合键有一些问题 .

我有那些 table

enter image description here

enter image description here

这就是我如何映射实体

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或EntitySets .

1 回答

  • 0

    我通过重新设计表来解决它 . 认为复合键是糟糕的设计 .

相关问题