Article ID: 2127505 - View products that this article applies to.
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.
Collapse this imageExpand this image
Download informationTo download this code sample, click the following link:
Technical overviewIn 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:
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.
LanguagesThis code sample contains the following programming languages:
Collapse this tableExpand this table
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 samplesTo find more All-In-One Code Framework samples, you can simply click the following link:
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)
(http://go.microsoft.com/fwlink/?LinkId=151500)for other considerations.