Uno a muchos en Entity Framework usando mapeo fluido

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>();

    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; }
    public virtual ProductGroup ProductGroup { get; set; }

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

public class ProductArticle : EntityBase
    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

Este es mi mapeo

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

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

But it doesnt work.. Please help :)

preguntado el 13 de septiembre de 13 a las 11:09

it doesnt work.... What exactly doesn't work? -

1 Respuestas

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)

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

Respondido el 13 de Septiembre de 13 a las 11:09

The problem with the conventions is that it all became more complicated to understand what is going on. If one extra line of code makes ir more explicit it is well worth. - MeTito

@Marco conventions is a problem only when you have little knowledge about tool you are using. Google for Convención sobre configuración. And I hope you'll stop specifying mappings for Id as a key property and User as a table name for User entity. Also each big project has it's own conventions. E.g. tables in database are named with tbl prefix. Instead of specifying table name for 100 tables normally you will use convention which developers know about. I can tell more about conventions, but try google yourself - sergey berezovskiy

I don't need to Google, I've been working in IT for over 10 years now, and I went past all the very verbose xml configurations files to no configuration at all, and all I am saying is that full convention is bad and it only works well in the beginning, as soon as you start adding more people to a project where a lot of conventions were created, that's when the issues start happening. So no, full convention over configuration is bad. - MeTito

And just for the record, I did not say convention over configuration, I said full convention with no configuration ;) - MeTito

@Marco I also have seen projects which were paid per line of code. Configuring everything manually when all default conventions worked well is the way guys coded there :) Obvious things should not be coded. If all things are obvious, nothing should be coded unless you are paid for each line. - sergey berezovskiy

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.