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.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.Friday, January 21, 2011 11:05 AM
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).
Friday, January 21, 2011 10:26 PMModerator
- Proposed as answer by Rowan MillerMicrosoft employee, Moderator Friday, January 21, 2011 10:26 PM
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
http://weblogs.asp.net/zeeshanhiraniSaturday, January 22, 2011 8:19 AM