简体   繁体   中英

One to many in Entity Framework using fluent mapping

Im trying to do a one-to-many map by using fluent api. This is my classes

    public class Product : EntityBase
{
    public Product()
    {
        this.ProductArticles = new List<ProductArticle>();
    }

    [Key]
    public int ProductId { get; set; }
    public string Description { 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 PriceIn { get; set; }
    public decimal PriceOut { get; set; }
    public decimal PriceArtisanIn { get; set; }
    public decimal PriceArtisanOut { get; set; }
    public decimal PriceTotalIn { get; set; }
    public decimal PriceTotalOut { get; set; }
    public decimal PriceTotalOutVat { get; set; }
    public decimal PriceAdjustment { get; set; }
    public bool Calculate { get; set; }
    public string Notes { get; set; }
    [ForeignKey("ProductGroupId")]
    public virtual ProductGroup ProductGroup { get; set; }

    public virtual ICollection<ProductArticle> ProductArticles { get; set; }
}

public class ProductArticle : EntityBase
{
    [Key]
    public int ProductArticleId { get; set; }
    public int ProductId { get; set; }
    public int ArticleId { get; set; }
    public decimal Qty { get; set; }
    public decimal PriceIn { get; set; }
    public bool Primary { get; set; }
    public virtual Product Product { get; set; }
    public virtual Article Article { get; set; }
}

Now i want from single Product include all ProductArticles

This is my mapping

    public class ProductMap : EntityTypeConfiguration<Product>
{
    public ProductMap()
    {
        // Primary Key
        this.HasKey(p => p.ProductId);

        // Table & Column Mappings
        this.ToTable("Product");
        this.HasMany(p => p.ProductArticles)
            .WithOptional()
            .Map(p => p.MapKey("ProductId").ToTable("ProductArticle"));
    }

But it doesnt work.. Please help :)

First - by convention EF treats property with name equal to Id or EntityTypeName + Id is a primary key. So, you don't need to configure that manually.

Second - if you don't want table names to be plural, just remove that convention from your context instead of providing table name for each entity mapping:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    base.OnModelCreating(modelBuilder);
}

And last - EF smart enough to define foreign keys which have names like RelatedEntityTypeName + Id . So, you don't need any fluent configurations here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM