What are Independent Associations and Foreign Key Associations? [duplicate]

Just my opinions about WHY to use independent or foreign key associations:

Independent associations

Pros:

  • This is correct way to go in object oriented world. In object oriented world we are using references inside our aggregates, not some magic keys.

Cons:

  • With pure POCO you don’t know if principal relation is really NULL or just not loaded because in both cases your reference is null. You must ask context to differ between those two nulls. This was not a problem with heavy EntityObject base entities where every navigation property to principal entity was paired with another property suffixed Reference providing some additional details about the relation.
  • EF’s way to manage independent associations is quite complex especially when it comes to attaching detached object graphs. Each independent association has its own state and it can never be in Modified state. Every modification always consists of setting old relation as deleted and creating new relation as added – this is a real mess when you try to use it.
  • It was reported that independent associations slow down view generation during EF’s initialization (or during view pregeneration) significantly.
  • Independent association can be much harder to use in data-binding scenarios where you need to bind just foreign key.

Foreign key associations

Pros:

  • Simple. Key properties are easy to manage and they solve all issues with independent associations – no state for foreign associations, direct data-binding, immediate visibility if relation exists (key is not null), etc.

Cons:

  • They are conceptually wrong and offering them in EF was a big step back from object world to relational world. I still believe that correct solution was improving or changing the way how independent associations are handled even if it could cause huge breaking changes between EFv1 and EFv4. I also don’t like current situation where we have two types of associations with completely different behavior. There should be only one type with well defined behavior and optional foreign key properties exposed on entity.

This difference matters only for one-to-many associations because one-to-one are always Foreign key associations and many-to-many are always independent associations.

Leave a Comment