none
EF 4 CTP 5 many-many conditional mapping

    Pregunta

  • Is it possible to have some kind of conditional mapping in many-many scenarios using the Code First fluent API?

    Let's say, we have a product table and 2 tables simplecategory and specialcategory.

    Both share one mapping table productcategories.

    This is the fluent configuration for the simplecategory relation:

     modelBuilder.Entity<Product>()
                     .HasMany(p => p.SimpleCategories)               
                     .WithMany(c=>c.Products)               
                     .Map(
                     mc =>
                     {
                         mc.ToTable("ProductCategories");
                         mc.MapLeftKey(p => p.Id, "ProductID");
                         mc.MapRightKey(c => c.Id, "CategoryID");                   
                     }
                     );

    For certain conditions, either values in the mapping table, or even better values in the simplecategory table,
    I would like to take the specialcategory mapping instead, or somehow inject the values from there in that categories collection of the product.

     

    viernes, 21 de enero de 2011 11:05

Todas las respuestas

  • Hi,

    This isn't supported in Code First (or EF in general). My suggestion would be to create a couple of views in your database to split up the records from the join table and then map two separate relationships using Code First (mapping to the views rather than the underlying table).

    ~Rowan

    viernes, 21 de enero de 2011 22:26
    Moderador
  • Hello *,

    I have a recipe in my book that covers exactly this concept of mapping many-many association with conditions. its called

    Creating Conditional Associations

    http://books.google.com/books?id=wB3ACQzTeRAC&lpg=PA538&ots=fkn_My-SFC&dq=Creating%20Conditional%20Associations%20entity%20framework%20recipes&pg=PA536#v=onepage&q&f=false


    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    sábado, 22 de enero de 2011 8:19