SIGN UP MEMBER LOGIN:    
ARTICLE

Association in Entity Framework

Posted by Jignesh Trivedi Articles | .NET 4.5 February 16, 2012
Association defines a relationship between two entities in Entity Framework. Association is defined in a conceptual model by the “Association” Element and each relationship contains two ends that describe the entity type and multiplicity type (one to one, zero-or –one, one to many, many to many).
Reader Level:
Download Files:
 

Association in Entity Framework

Association defines a relationship between two entities in Entity Framework. Association is defined in a conceptual model by the "Association" Element and each relationship contains two ends that describe the entity type and multiplicity type (one to one, zero-or -one, one to many, many to many). The relationship may be managed by the "Referential" constraint. Referential Constrains contains information about a principal role and a dependent role in a relationship.

Entity Framework supports two type of association: Foreign key columns Association and Independent association.

Foreign key association

You may create or change a relationship by modifying the foreign key value of the dependent object when the foreign key properties are included in EDM. This type of association is called a foreign key association. In this type of association, a foreign key must be exposed.
Example:



In this current example, a foreign key is defined between Customer and People (Person in EDM) in the storage model, which means the foreign key is defined in the database.

The following are the storage model and conceptual model. In both storage and conceptual models the foreign key is defined. In the following sample association set, the association and navigation properties are automatically bound or created.

<edmx:StorageModels>
    <
Schema Namespace="AdventureWorksModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008"
xmlns:store
="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl"
>

        <
EntityContainer Name="AdventureWorksModelStoreContainer">
..........
..........

<AssociationSet Name="FK_Customer_Person" Association="AdventureWorksModel.Store.FK_Customer_Person">
            <
End Role="Person" EntitySet="Person" />
            <
End Role="Customer" EntitySet="Customer" />
          </
AssociationSet>
        </
EntityContainer>
...........
...........

<
Association Name="FK_Customer_Person">
          <
End Role="Person" Type="AdventureWorksModel.Store.Person" Multiplicity="1" />
          <
End Role="Customer" Type="AdventureWorksModel.Store.Customer" Multiplicity="0..1" />
          <
ReferentialConstraint>
            <
Principal Role="Person">
              <
PropertyRef Name="PersonId" />
            </
Principal>
            <
Dependent Role="Customer">
              <
PropertyRef Name="CustomerID" />
            </
Dependent>
          </
ReferentialConstraint>
        </
Association>
.......

.......
</
Schema></edmx:StorageModels>
<
edmx:ConceptualModels>
      <
Schema Namespace="AdventureWorksModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
xmlns
="http://schemas.microsoft.com/ado/2008/09/edm"
>

        <
EntityContainer Name="AdventureWorksEntities" annotation:LazyLoadingEnabled="true">
.........
.........
<
AssociationSet Name="FK_Customer_Person" Association="AdventureWorksModel.FK_Customer_Person">
            <
End Role="Person" EntitySet="People" />
            <
End Role="Customer" EntitySet="Customers" />
          </
AssociationSet>
.......

.......
<
Association Name="FK_Customer_Person">
          <
End Type="AdventureWorksModel.Person" Role="Person" Multiplicity="1" />
          <
End Type="AdventureWorksModel.Customer" Role="Customer" Multiplicity="0..1" />
          <
ReferentialConstraint>
            <
Principal Role="Person">
              <
PropertyRef Name="PersonId" />
            </
Principal>
            <
Dependent Role="Customer">
              <
PropertyRef Name="CustomerID" />
            </
Dependent>
          </
ReferentialConstraint>
        </
Association>
.......
.......
        </
Schema>
    </
edmx:ConceptualModels>

Independent association

In Independent association, there is a foreign key property missing from the storage Model (dependent entity). In other words, in this type of association there is no physical foreign key in the database. The relation between two entities is defined as a separate object and handle by the Object State Manager. It has its own object state which must be handled correctly. Here both entities work as detached entities, so it must be handled correctly. When creating a new relationship, the new association must have entities at both ends. The Entity Framework allows us to define a relationship in the conceptual Model.

Example :

In the following example, there are two tables; Employee and Department. An employee must have a department. But a relationship between Employee and Department is not defined in the physical layer (which means the foreign key is not defined in the database).



 

In independent association, the foreign key (Association) is defined in the conceptual model.

To define the association in a conceptual model, we must add association set, association and navigation properties.

The steps for defining an association.

Step 1 :  Define the Association Set in the entity container.

<AssociationSet Name="FK_Employees_Departments" Association="AdventureWorksModel.FK_Employees_Departments">

            <End Role="Department" EntitySet="Departments" />

            <End Role="Employee" EntitySet="Employees" />

</AssociationSet>

 Step 2 : Define the association.

<Association Name="FK_Employees_Departments">
       <
End Type="AdventureWorksModel.Department" Role="Department" Multiplicity="1" />
       <
End Type="AdventureWorksModel.Employee" Role="Employee" Multiplicity="*" />
       <
ReferentialConstraint>
          <
Principal Role="Department">
<
PropertyRef Name="DepartmentId" />
          </
Principal>
          <
Dependent Role="Employee">
            <
PropertyRef Name="DepartmentId" />
          </
Dependent>
       </
ReferentialConstraint>
</
Association>

Step 3 : Define the navigation property in each entity type in a concepual model.



 
<NavigationProperty Name="Emplyees" Relationship="AdventureWorksModel.FK_Employees_Departments" FromRole="Department" ToRole="Employee" />
<
NavigationProperty Name="Deparment" Relationship="AdventureWorksModel.FK_Employees_Departments" FromRole="Employee" ToRole="Department"
/>


Conclusion : The Entity Framework allows us to define a relationship (association) in a conceptual model with a defined foreign key in the physical layer (database).

Login to add your contents and source code to this article
share this article :
post comment
 

Thank you very much for the article.

Posted by Jessica Stephen Feb 16, 2012

Its a very nice and useful article.

Posted by Monika Arora Feb 16, 2012

Comprehensively explained

Posted by Laura Parker Feb 16, 2012

Hi, Jignesh it's well explanation about entity framework, So keep going and thanks for sharing.......

Posted by Amit Maheshwari Feb 16, 2012

This article is so valuable, keep it up and thanks for sharing.

Posted by Nitin Singh Feb 16, 2012
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor