Association in Entity Framework

Association in Entity Framework

Association defines a relationship between two entities in an Entity Framework. The 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

  1. Foreign key columns Association
  2. 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

Foreign key

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 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 handled 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. However, a relationship between the Employee and the Department is not defined in the physical layer (which means the foreign key is not defined in the database).

Independent association

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.

<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 conceptual model.

 Conceptual model

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

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