Foreign key vs. Independent associations in Entity Framework 4(www.ladislavmrnka.com)

submitted by LadislavMrnkaLadislavMrnka(65) 8 months, 14 days ago

Independent and Foreign key associations are two ways to model relationships in Entity framework 4. This article describes differences between them.

4 comments |category: |Views: 54

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by dpetersondpeterson(4338) 8 months, 14 days ago +1

I'm in the first camp. I think that foreign key relationships should be modeled in the contextual model because they are there for a reason. They represent a real-world relationship, and that relationship should carry through to your conceptual model. If this is not the case I would argue that "you're doing it wrong" ;-)

I'm sure there are exceptions to that, but I'm no stranger to database design and data-driven applications, and I've never created a foreign key that didn't have a purpose. Definitely not one that existed only to satisfy the database and be discarded elsewhere in my architecture.

Do you perhaps have an example you can share with us when it's preferable to ignore the fact that there's a foreign key association in the database? What do you do about error-handling in that case, and how is it preferable to catching the foreign key error raised by EF?

Thanks for the article!

Reply

posted by vijaystvijayst(1311) replied to dpetersondpeterson(4338), 8 months, 13 days ago 0

The article is well written. It is a good read.

I am not sure why having two types of association can be a design flaw in Entity framework. Does it not give an option for the developers to choose one over another?

Reply

posted by LadislavMrnkaLadislavMrnka(65) replied to vijaystvijayst(1311), 8 months, 12 days ago 0

Thanks for reading and comments guys!

I think that exposing foreign keys in EF is not needed. Foreign keys are just database way to make relations between principal and dependent record. In object oriented world we already have such construct - it is object reference which is already provided by EF through navigation properties and it should be job of persistence layer (ORM = EF) to translate these references to correct foreign keys used in the database.

But as I described in my article I believe that choice if a foreign key should be exposed in an entity as a property or not should be left to developer and not demanded by framework as happened with independent associations. The reason why I don't like foreign key associations in EF is the way how they are implemented. It is not just an extension to independent association which would allow you exposing a foreign key property. It is instead completely new feature with completely different behaviour, usage and change tracking - that is what I call design flaw because it leads to confusions and complications.

Reply

posted by vijaystvijayst(1311) replied to LadislavMrnkaLadislavMrnka(65), 8 months, 12 days ago 0

Thank you for the clarifications.

Reply

information Login or create an account to comment on this story