EF 4 CTP 5 many-many conditional mapping


  • 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:

                     .HasMany(p => p.SimpleCategories)               
                     mc =>
                         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).


    viernes, 21 de enero de 2011 22:26
  • 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

    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    sábado, 22 de enero de 2011 8:19