Association in Entity Framework

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 Constraints contain information about a principal role and a dependent role in a relationship.
Entity Framework supports two types 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.
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.
  1. <edmx:StorageModels>  
  2.     <Schema Namespace="AdventureWorksModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="" xmlns="">  
  3.         <EntityContainer Name="AdventureWorksModelStoreContainer">  
  4.             ..........  
  5.             ..........  
  6.             <AssociationSet Name="FK_Customer_Person" Association="AdventureWorksModel.Store.FK_Customer_Person">  
  7.                 <End Role="Person" EntitySet="Person" />  
  8.                 <End Role="Customer" EntitySet="Customer" />  
  9.             </AssociationSet>  
  10.         </EntityContainer>  
  11.         ...........  
  12.         ...........  
  13.         <Association Name="FK_Customer_Person">  
  14.             <End Role="Person" Type="AdventureWorksModel.Store.Person" Multiplicity="1" />  
  15.             <End Role="Customer" Type="AdventureWorksModel.Store.Customer" Multiplicity="0..1" />  
  16.             <ReferentialConstraint>  
  17.                 <Principal Role="Person">  
  18.                     <PropertyRef Name="PersonId" />  
  19.                 </Principal>  
  20.                 <Dependent Role="Customer">  
  21.                     <PropertyRef Name="CustomerID" />  
  22.                 </Dependent>  
  23.             </ReferentialConstraint>  
  24.         </Association>  
  25.         .......  
  26.         .......  
  27.     </Schema>  
  28. </edmx:StorageModels>  
  29. <edmx:ConceptualModels>  
  30.     <Schema Namespace="AdventureWorksModel" Alias="Self" xmlns:annotation="" xmlns="">  
  31.         <EntityContainer Name="AdventureWorksEntities" annotation:LazyLoadingEnabled="true">  
  32.             .........  
  33.             .........  
  34.             <AssociationSet Name="FK_Customer_Person" Association="AdventureWorksModel.FK_Customer_Person">  
  35.                 <End Role="Person" EntitySet="People" />  
  36.                 <End Role="Customer" EntitySet="Customers" />  
  37.             </AssociationSet>  
  38.             .......  
  39.             .......  
  40.             <Association Name="FK_Customer_Person">  
  41.                 <End Type="AdventureWorksModel.Person" Role="Person" Multiplicity="1" />  
  42.                 <End Type="AdventureWorksModel.Customer" Role="Customer" Multiplicity="0..1" />  
  43.                 <ReferentialConstraint>  
  44.                     <Principal Role="Person">  
  45.                         <PropertyRef Name="PersonId" />  
  46.                     </Principal>  
  47.                     <Dependent Role="Customer">  
  48.                         <PropertyRef Name="CustomerID" />  
  49.                     </Dependent>  
  50.                 </ReferentialConstraint>  
  51.             </Association>  
  52.             .......  
  53.             .......  
  54.     </Schema>  
  55. </edmx:ConceptualModels> 

Independent association

In an 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).
An independent association, the foreign key (Association) is defined in the conceptual model.
To define the association in a conceptual model, we must add an association set, association, and navigation properties.
The steps for defining an association.
Step 1:  Define the Association Set in the entity container.
  1. <AssociationSet Name="FK_Employees_Departments" Association="AdventureWorksModel.FK_Employees_Departments">  
  2.             <End Role="Department" EntitySet="Departments" />  
  3.             <End Role="Employee" EntitySet="Employees" />  
  4. </AssociationSet> 
Step 2 : Define the association.
  1. <Association Name="FK_Employees_Departments">  
  2.        <End Type="AdventureWorksModel.Department" Role="Department" Multiplicity="1" />  
  3.        <End Type="AdventureWorksModel.Employee" Role="Employee" Multiplicity="*" />  
  4.        <ReferentialConstraint>  
  5.           <Principal Role="Department">  
  6. <PropertyRef Name="DepartmentId" />  
  7.           </Principal>  
  8.           <Dependent Role="Employee">  
  9.             <PropertyRef Name="DepartmentId" />  
  10.           </Dependent>  
  11.        </ReferentialConstraint>  
  12. </Association> 
Step 3: Define the navigation property in each entity type in a conceptual model.
  1. <NavigationProperty Name="Emplyees" Relationship="AdventureWorksModel.FK_Employees_Departments" FromRole="Department" ToRole="Employee" />  
  2. <NavigationProperty Name="Deparment" Relationship="AdventureWorksModel.FK_Employees_Departments" FromRole="Employee" ToRole="Department" /> 


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