Labels

Thursday, June 4, 2009

LINQ to Entity - Entity Framework (EF)

Scope -

 

-          ADO.NET Entity Framework

 

What is ADO.NET Entity Framework?

 

-          EF is a platform, implementing the Entity Data Model (EDM), which provides a higher level of abstraction when developing against databases.

 

-          EF is more than a simple ORM tool – As it allow developers to work against a Conceptual Model with a very Flexible Mapping.

-          The nucleus of EF is its layer of abstraction that is divided into conceptual, mapping, and logical layers making up the EDM.

-          In addition, EF utilizes two APIs, Object Services and the Entity Client, for working with the EDM.

-          EF also uses two data manipulation constructs, Entity SQL (ESQL) and LINQ to Entities.

 

-          The Entity Client API is a lower-level .NET data provider, which provides a mechanism for interacting directly with your Conceptual Model by using the new query language, ESQL.

-          The Object Services API is principally built on top of the entity client provider and provides the functionality to query in ESQL along with LINQ to Entities (that is, object queries).

-          Object Services supports two execution models against the EDM: LINQ to Entities and ESQL.

 

 

What is EDM -

 

EDM is broken into three metadata structures that make up the Conceptual, Mapping, and Logical layers used by EF.

These structures are classified as the design schema (your .edmx file) and consist of the following aspects:

 

Conceptual Schema Definition Language (): CSDL

Stored Schema Definition Language ():  SSDL

Mapping Specification Language (): MSL

 

 

CSDL

-          The CSDL represents the conceptual model of your data.

-          This XML structure includes your entities and their relationships and represents your object code.

 

SSDL

-          The SSDL represents the database structure and the data in your database.

-          This XML structure is used to describe the logical layer of your solution.

 

MSL

-          The MSL is metadata that maps the conceptual model described in the CSDL to the logical model described in the SSDL.

-          This XML structure is your mapping information that connects your entities to your database.

 

 

 

Model Domain Objects with the Entity Framework: Figure 2: Go with the Entity Framework

 

 

CSDL –

 

Here, for the below explanation we are considerign only two tables.

 

-          Department – Parent Table

-          EmployeeDepartmentHistory – Child Table

 

 

<edmx:ConceptualModels>

  <Schema Namespace="AdventureWorksModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">

 

    <EntityContainer Name="AdventureWorksEntities">

      <EntitySet Name="Department" EntityType="AdventureWorksModel.Department" />

      <EntitySet Name="EmployeeDepartmentHistory" EntityType="AdventureWorksModel.EmployeeDepartmentHistory" />

      <AssociationSet Name="FK_EmployeeDepartmentHistory_Department_DepartmentID"

                              Association="AdventureWorksModel.FK_EmployeeDepartmentHistory_Department_DepartmentID">

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

        <End Role="EmployeeDepartmentHistory" EntitySet="EmployeeDepartmentHistory" />

      </AssociationSet>

    </EntityContainer>

 

   <EntityType Name="Department">

      <Key>

        <PropertyRef Name="DepartmentID" />

      </Key>

      <Property Name="DepartmentID" Type="Int16" Nullable="false" />

      <Property Name="Name" Type="String" Nullable="false" MaxLength="50" />

      <Property Name="GroupName" Type="String" Nullable="false"  MaxLength="50" />

      <Property Name="ModifiedDate" Type="DateTime" Nullable="false" />

      <NavigationProperty Name="EmployeeDepartmentHistory"                                                                                           

                                    Relationship="AdventureWorksModel.FK_EmployeeDepartmentHistory_Department_DepartmentID"

                                    FromRole="Department" ToRole="EmployeeDepartmentHistory" />

    </EntityType>

 

    <EntityType Name="EmployeeDepartmentHistory">

      <Key>

        <PropertyRef Name="EmployeeID" />

        <PropertyRef Name="DepartmentID" />

        <PropertyRef Name="ShiftID" />

        <PropertyRef Name="StartDate" />

      </Key>

      <Property Name="EmployeeID" Type="Int32" Nullable="false" />

      <Property Name="DepartmentID" Type="Int16" Nullable="false" />

      <Property Name="ShiftID" Type="Byte" Nullable="false" />

      <Property Name="StartDate" Type="DateTime" Nullable="false" />

      <Property Name="EndDate" Type="DateTime" />

      <Property Name="ModifiedDate" Type="DateTime" Nullable="false" />

      <NavigationProperty Name="Department"

                                    Relationship="AdventureWorksModel.FK_EmployeeDepartmentHistory_Department_DepartmentID"

                                    FromRole="EmployeeDepartmentHistory" ToRole="Department" />

    </EntityType>

 

    <Association Name="FK_EmployeeDepartmentHistory_Department_DepartmentID"// One to Many

      <End Role="Department" Type="AdventureWorksModel.Department" Multiplicity="1" />

      <End Role="EmployeeDepartmentHistory" Type="AdventureWorksModel.EmployeeDepartmentHistory" Multiplicity="*" />

      <ReferentialConstraint>

        <Principal Role="Department">

          <PropertyRef Name="DepartmentID" />

        </Principal>

        <Dependent Role="EmployeeDepartmentHistory">

          <PropertyRef Name="DepartmentID" />

        </Dependent>

      </ReferentialConstraint>

    </Association>

 

  </Schema>

</edmx:ConceptualModels>

 

 

 

SSDL –

 

 

<edmx:StorageModels>

  <Schema Namespace="AdventureWorksModel.Store" Alias="Self"

  xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">

    <EntityContainer Name="HumanResources">

      <EntitySet Name="Department" EntityType="AdventureWorksModel.Store.Department" />

      <EntitySet Name="EmployeeDepartmentHistory" EntityType="AdventureWorksModel.Store.EmployeeDepartmentHistory" />

      <AssociationSet  Name="FK_EmployeeDepartmentHistory_Department_DepartmentID"

      Association="AdventureWorksModel.Store.FK_EmployeeDepartmentHistory_Department_DepartmentID">

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

        <End Role="EmployeeDepartmentHistory" EntitySet="EmployeeDepartmentHistory" />

      </AssociationSet>

    </EntityContainer>

 

    <EntityType Name="Department">

      <Key>

        <PropertyRef Name="DepartmentID" />

      </Key>

      <Property Name="DepartmentID" Type="smallint" Nullable="false" StoreGeneratedPattern="identity" />

      <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="50" />

      <Property Name="GroupName" Type="nvarchar"  Nullable="false" MaxLength="50" />

      <Property Name="ModifiedDate" Type="datetime" Nullable="false" />

    </EntityType>

 

    <EntityType Name="EmployeeDepartmentHistory">

      <Key>

        <PropertyRef Name="EmployeeID" />

        <PropertyRef Name="StartDate" />

        <PropertyRef Name="DepartmentID" />

        <PropertyRef Name="ShiftID" />

      </Key>

      <Property Name="EmployeeID" Type="int" Nullable="false" />

      <Property Name="DepartmentID" Type="smallint" Nullable="false" />

      <Property Name="ShiftID" Type="tinyint" Nullable="false" />

      <Property Name="StartDate" Type="datetime" Nullable="false" />

      <Property Name="EndDate" Type="datetime" />

      <Property Name="ModifiedDate" Type="datetime" Nullable="false" />

    </EntityType>

 

    <Association Name="FK_EmployeeDepartmentHistory_Department_DepartmentID">

      <End Role="Department" Type="AdventureWorksModel.Store.Department" Multiplicity="1" />

      <End Role="EmployeeDepartmentHistory" Type="AdventureWorksModel.Store.EmployeeDepartmentHistory" Multiplicity="*" />

      <ReferentialConstraint>

        <Principal Role="Department">

          <PropertyRef Name="DepartmentID" />

        </Principal>

        <Dependent Role="EmployeeDepartmentHistory">

          <PropertyRef Name="DepartmentID" />

        </Dependent>

      </ReferentialConstraint>

    </Association>

  </Schema>

</edmx:StorageModels>

 

 

 

 

MSL –

 

 

<edmx:Mappings>

  <Mapping Space="C-S"

  xmlns="urn:schemas-microsoft com:windows:storage:mapping:CS">

    <EntityContainerMapping StorageEntityContainer="HumanResources"  CdmEntityContainer="AdventureWorksEntities">

      <EntitySetMapping Name="Department">

        <EntityTypeMapping TypeName="IsTypeOf(AdventureWorksModel.Department)">

          <MappingFragment StoreEntitySet="Department">

            <ScalarProperty Name="DepartmentID" ColumnName="DepartmentID" />

            <ScalarProperty Name="Name" ColumnName="Name" />

            <ScalarProperty Name="GroupName" ColumnName="GroupName" />

            <ScalarProperty Name="ModifiedDate" ColumnName="ModifiedDate" />

          </MappingFragment>

        </EntityTypeMapping>

      </EntitySetMapping>

 

      <EntitySetMapping Name="EmployeeDepartmentHistory">

        <EntityTypeMapping TypeName="IsTypeOf(AdventureWorksModel.EmployeeDepartmentHistory)">

          <MappingFragment StoreEntitySet="EmployeeDepartmentHistory">

            <ScalarProperty Name="EmployeeID" ColumnName="EmployeeID" />

            <ScalarProperty Name="DepartmentID" ColumnName="DepartmentID" />

            <ScalarProperty Name="ShiftID" ColumnName="ShiftID" />

            <ScalarProperty Name="StartDate" ColumnName="StartDate" />

            <ScalarProperty Name="EndDate" ColumnName="EndDate" />

            <ScalarProperty Name="ModifiedDate" ColumnName="ModifiedDate" />

          </MappingFragment>

        </EntityTypeMapping>

      </EntitySetMapping>

 

      <AssociationSetMapping

            Name="FK_EmployeeDepartmentHistory_Department_DepartmentID"

            TypeName="AdventureWorksModel.FK_EmployeeDepartmentHistory_Department_DepartmentID"

            StoreEntitySet="EmployeeDepartmentHistory">

        <EndProperty Name="Department">

          <ScalarProperty Name="DepartmentID" ColumnName="DepartmentID" />

        </EndProperty>

        <EndProperty Name="EmployeeDepartmentHistory">

          <ScalarProperty Name="EmployeeID" ColumnName="EmployeeID" />

          <ScalarProperty Name="StartDate" ColumnName="StartDate" />

          <ScalarProperty Name="DepartmentID" ColumnName="DepartmentID" />

          <ScalarProperty Name="ShiftID" ColumnName="ShiftID" />

        </EndProperty>

        <Condition ColumnName="DepartmentID" IsNull="false" />

      </AssociationSetMapping>

    </EntityContainerMapping>

  </Mapping>

</edmx:Mappings>

 

 

 

 

Thanks & Regards,

Arun Manglick || Senior Tech Lead

 

No comments:

Post a Comment