Code through the pain Ladislav Mrnka's professional blog about software development


The first version of Entity Framework introduced in .NET 3.5 SP1 (EFv1) contained only a single type of association between entities. All associations in EFv1 was based on the correct object oriented approach where a relation between entities is modelled as a reference between objects. The problem of this association type was the internal implementation in Entity framework which makes it sometimes (especially in detached scenarios) very hard to use. The next version of Entity Framework introduced in .NET 4.0 (EFv4) included a new type of association where conceptual model also includes foreign key properties to define connections between entities. To differ between these two types of associations ADO.NET team decided to call the old type Independent association and the new type Foreign key association.

The question is: Was introducing the new type of association a good design choice? I believe that Foreign key associations should not never be included and instead Independent association should be improved. The main reasons for introducing the new type of association are covered by the article on Entity Framework Design blog. The community was obviously divided between two groups where one group requested foreign keys whereas second group didn't want them. I'm the member of the second group because in my opinion ORM tool should not push relational concepts to the conceptual / object model. A foreign key is definitely a relational concept to model a relation and we rarely want it to pollute our object model.

Despite of that I agree with ADO.NET team that the final decision should be left to developers so if we want to include a foreign key property in our entity we should be able to do that. I don't agree with the way how it was achieved. The biggest problem is that a difference between Independent and Foreign key associations is not only in the way how they use foreign keys. These two association types are handled by EFv4 completely differently. The rest of the article will explain differences between these two association types and it will also mention how this affects Entity Framework 4.1 and its DbContext API.

Posted on May 8, 2011 by Ladislav Mrnka
Filed under: Entity framework
Continue reading