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.
To download this code sample, click the following link:
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.
ADO.NET Entity Framework
This code sample contains the following programming languages:
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: http://1code.codeplex.com
How to find more All-In-One Code Framework samples