Home > Entity Framework, LINQ > Entity Framework assications (Independent vs FK)

Entity Framework assications (Independent vs FK)

Entity Framework association is a different concept to FKs in the database schema.  Supporting or not supporting the Schema FKs has been a topic for debate in the community until now. Faisal Mohamood (Program Manager of Entity Framework) believes in the following prons and cons of using FKs in a model (Ref 1):

Benefits of Foreign Keys

  1. Keeps it simple (for the simple cases)  and allows you to deal with relationship like you deal with them in the database
  2. Technically, you can update relationships without having both ends loaded/materialized. This is however in reality not always interesting since you will likely load both ends but this feature is definitely useful.

Disadvantages of Foreign Keys in the Model

  1. It is a part of the impedance mismatch problem.
  2. It doesn’t allow the concepts that you would expect from relationships in objects (easily getting from one end to the other) for instance.
  3. Having foreign keys as well as object references for relationship navigation presents the problem of two different artifacts representing relationships – this introduces complexity and now you have to make sure that you keep these two in sync.

I think the biggest disadvantage of FKs (stated in number 2) is the conflict with the object model pattern.

However, it seems that Microsoft decided that both sides of this discussion have their valid points. So in Entity Framework .Net 4, Fks are supported and it is up to the software architects to make their decision to just use  independent associations (.Net 3.5 SP1) or add the FKs to the model.

The following code snippets form ADO.Net blog illustrate the implication of using Fks as part of the model (Ref 2). In our model there are two entities Product and Category, with the association one to many where each Product belongs to one Category.

First:  Using the independent association between Product and Category.

public void Create_new_Product_in_existing_Category_by_reference()
    using (var context = new Context())
        //Create a new product and relate to an existing category
        Product p = new Product
            ID = 1,
            Name = "Bovril",
            Category = context.Categories
                        .Single(c => c.Name == "Food")
        // Note: no need to add the product, because relating
        // to an existing category does that automatically.
        // Also notice the use of the Single() query operator
        // this is new to EF in .NET 4.0 too.

Second: Using the Foreign Key.

using (var context = new Context())
    //Create a product and a relationship to a known category by ID
    Product p = new Product
        ID = 1,
        Name = "Bovril",
        CategoryID = 13
    //Add the product (and create the relationship by FK value)

Further Reading:


  1. [blogs.msdn.com/EFDesign]Foreign Keys in the Conceptual and Object Models
  2. [blogs.msdn.com/EFDesign]Foreign Keys in the Entity Framework
Categories: Entity Framework, LINQ
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: