How to use Foreign Key Association and Independent Association in Entity Framework 4.0

This article has been archived. It is offered "as is" and will no longer be updated.
This article describes an All-In-One Code Framework sample that is available for downloading.

This sample demonstrates one of the new features of Entity Framework 4.0 (EF4) – Foreign Key Association. It compares the new Foreign Key Association with the Independent Association and shows how to insert new related entities, how to insert by existing entities and how to update existing entities with these two associations.

Difficulty level

Download information

To download this code sample, click the following link:


Technical overview

In the Entity Framework, an entity can be related to other entities through an association. This relationship between entities is defined in the conceptual model by the Association element. Each relationship contains two ends that describe the entity type and the multiplicity type (one, zero-or-one, or many). The relationship may be governed by a referential constraint. It describes which end in the relationship is a principal role and which end in the relationship is a dependent role.

Starting with the .NET Framework version 4, you can include foreign keys in the conceptual model. The “Include foreign key columns in the model” option in the Entity Data Model Wizard is selected by default. When this option is selected, the generated entity objects have scalar properties that map to foreign key columns.. You are able to create or change a relationship by modifying the foreign key value on a dependent object while the foreign key properties are included. This type of association is called a foreign key association.

The association information is managed as an independent object when foreign key columns are not included in the conceptual model. Relationships are tracked through object references instead of foreign key properties, and represented as an ObjectStateEntry object in the ObjectStateManager. This type of association is called an independent association. The most common way to modify an independent association is to modify the navigation properties that are generated for each entity that participates in the association.

In both types of associations, each object can have a navigation property for each relationship in which it participates. The navigation properties allow you to navigate and manage relationships in both directions:
  • Returning a reference object when the multiplicity type is either one or zero-or-one.
  • Returning a collection of objects when the multiplicity type is many.

You can choose to use one or both types of associations in your model. However, if you include a table that contains only foreign keys (also called a pure join table) in your model, an independent association will be used to manage the pure many-to-many relationship even if you specified to use a foreign key association in your model. The Entity Data Model Wizard does not create an entity that maps to a pure join table.

NoteFor more information about how to create the sample application and how to deploy the sample application, see the Readme.txt file that is included in the download package.

Technology category

  • ADO.NET Entity Framework


This code sample contains the following programming languages:

LanguageProject Name
Visual C#CSEFForeignKeyAssociation
Visual Basic.NETVBEFForeignKeyAssociation


  • You must attach the database file (EFDemoDB.mdf) that is included in the download package and then modify the connection string in the App.config file according to your database instance and login account.
  • This sample application was created by using Visual Studio 2010 RTM.


  • Entity Framework, Foreign Key Association, Independent Association

What is All-In-One Code Framework?

All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, composed, and documented to show one common code scenario. For more information about All-In-One Code Framework, visit the following Web site:

How to find more All-In-One Code Framework samples

To find more All-In-One Code Framework samples, you can simply click the following link:

Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use for other considerations.

Article ID: 2127505 - Last Review: 12/12/2015 08:48:02 - Revision: 2.0

Microsoft Visual Studio 2010 Professional, Microsoft Visual Studio 2010 Ultimate

  • kbnosurvey kbarchive kbcodefx kbrapidpub kbnomt atdownload kbsurveynew kbinfo KB2127505