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

Article ID: 2127505 - View products that this article applies to.
Expand all | Collapse all

On This Page

INTRODUCTION

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

Collapse this imageExpand this image
codefx Beginner

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

Languages

This code sample contains the following programming languages:

Collapse this tableExpand this table
LanguageProject Name
Visual C#CSEFForeignKeyAssociation
Visual Basic.NETVBEFForeignKeyAssociation

Prerequisites

  • 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.

Tag

  • Entity Framework, Foreign Key Association, Independent Association

MORE INFORMATION

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:
http://1code.codeplex.com

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:
http://support.microsoft.com/gp/gp_kbcodefx

REFERENCES

For more information, visit the following Web sites:

Defining and Managing Relationships (Entity Framework)

How to: Use EntityReference Object to Change Relationships between Objects (Entity Framework)

How to: Use the Foreign Key Property to Change Relationships between Objects

Foreign Keys in the Entity Framework (EF design blog)

Foreign Keys in the Conceptual and Object Models (EF design blog)

Foreign Key Relationships in the Entity Framework (ADO.NET team blog)


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.

Properties

Article ID: 2127505 - Last Review: July 20, 2010 - Revision: 2.0
APPLIES TO
  • Microsoft Visual Studio 2010 Professional
  • Microsoft Visual Studio 2010 Ultimate
Keywords: 
kbcodefx kbrapidpub kbnomt atdownload kbsurveynew kbinfo KB2127505

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com