I have read several articles and questions on concept of foreign key vs independent relationship when using Entity Framework. And I am still not 100% sure which way to go.... I would prefer not to "pollute" my domain POCOs by having a property that will be used in FK relationship when I already have a property reference to "has a" object.
My questions are (looking at you @EFTeam, @Ladislav Mrnka)
- are there any improvements on this subject in the upcoming Entity Framework v5?
- are there more advantages if I use FK instead of independent associations (particularly with code first)?
There are no improvements. Both options still exist. Advantage of using FK association is simplified handling of changes in one-to-many relation when working with disconnected object graphs. It should also simplify data binding if you use it.
If you have a large model, you're bound to "pollute" your domain objects (or conceptual model in general) anyway. For models with FK-mapped associations, the cost of "view generation" - a stage in the EF processing pipeline necessary to execute queries or save changes, one that can be moved to build-time ("pre-generating views") - is lower compared to models with independent associations. This is important because the amount of time to perform it may be unnoticeable for small models, but it gets longer very fast, especially when there are associations mapped to nullable foreign keys (to-0..1, or to-1 with derived entities in TPH mapped hierarchies). In the official EF5 performance considerations document an example difference in view generation time for a very large model is given as between "over a month and then we gave up" (with independent associations) and 104 minutes (with FK-mapped associations). In my case (several hundred highly connected entities) it's between 25 minutes and 40 seconds. The situation is the same in EF5 as it was in the previous versions.