Mapping a Conceptual Model to a Storage Model

The Entity Framework uses a conceptual model to provide an object-centric view of data, expressed as entity types and associations. An application developer only has to think about programming against the classes that are generated from the conceptual model, rather than having to also think about the store schema and how to access objects in the data store and transform them into programming objects. The Entity Framework uses a conceptual model, storage model, and mapping between these models to transform create, read, update, and delete operations against entities into equivalent operations in the data source.

Note

All the mapping file fragments that are shown in this section are generated by the Entity Data Model Wizard.

The Conceptual Model

The conceptual model for an application expresses entities and relationships in conceptual schema definition language (CSDL), which is an implementation of the Entity Data Model. CSDL is an XML-based language. Entity types defined in CSDL each have a name, a key for uniquely identifying instances, and a set of properties. The data types assigned to properties are specified as either simple types, which are scalar properties, or as complex types, which are types that consist of one or more scalar or complex properties. XML attributes may also specify nullability or assign a default value. Associations define the relationships between entities. Entity Framework language elements and terminology are explained in more detail in Entity Framework Terminology.

The following XML fragment represents part of the School conceptual model (based on the School example database). The example shows the Course and Department entity types that are related by the FK_Course_Department association, with other entities and associations removed.

<Schema Namespace="SchoolModel" Alias="Self" 
xmlns:annotation="https://schemas.microsoft.com/ado/2009/02/edm/annotation" 
      xmlns="https://schemas.microsoft.com/ado/2008/09/edm">
  <EntityContainer Name="SchoolEntities">
   <EntitySet Name="Courses" EntityType="SchoolModel.Course" />
   <EntitySet Name="Departments" EntityType="SchoolModel.Department" />

  <AssociationSet Name="FK_Course_Department" 
                  Association="SchoolModel.FK_Course_Department">
            <End Role="Department" EntitySet="Departments" />
            <End Role="Course" EntitySet="Courses" />
  </AssociationSet>

  </EntityContainer>
  <EntityType Name="Course">
       <Key>
         <PropertyRef Name="CourseID" />
       </Key>
       <Property Name="CourseID" Type="Int32" Nullable="false" />
       <Property Name="Title" Type="String" Nullable="false" 
                 MaxLength="100" Unicode="true" FixedLength="false" />
       <Property Name="Credits" Type="Int32" Nullable="false" />
       <Property Name="DepartmentID" Type="Int32" Nullable="false" />
       <NavigationProperty Name="Department" 
                       Relationship="SchoolModel.FK_Course_Department" 
                       FromRole="Course" ToRole="Department" />
  </EntityType>

  <EntityType Name="Department">
       <Key>
         <PropertyRef Name="DepartmentID" />
       </Key>
       <Property Name="DepartmentID" Type="Int32" Nullable="false" />
       <Property Name="Name" Type="String" Nullable="false" 
                 MaxLength="50" Unicode="true" FixedLength="false" />
       <Property Name="Budget" Type="Decimal" Nullable="false" 
                 Precision="19" Scale="4" />
       <Property Name="StartDate" Type="DateTime" Nullable="false" />
       <Property Name="Administrator" Type="Int32" />
       <NavigationProperty Name="Courses" 
                       Relationship="SchoolModel.FK_Course_Department" 
                       FromRole="Department" ToRole="Course" />
  </EntityType>
       
  <Association Name="FK_Course_Department">
    <End Role="Department" Type="SchoolModel.Department" 
         Multiplicity="1" />
    <End Role="Course" Type="SchoolModel.Course" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="Department">
              <PropertyRef Name="DepartmentID" />
            </Principal>
            <Dependent Role="Course">
              <PropertyRef Name="DepartmentID" />
            </Dependent>
          </ReferentialConstraint>
        </Association>

      </Schema>

The Storage Model

The storage model is described with store schema definition language (SSDL). The data types of properties declared in SSDL are those of the storage model. This storage model fragment shows an example of storage metadata for the Course and Department tables in the School database that are related by the FK_Course_Department foreign key, with other entities removed.

<Schema Namespace="SchoolModel.Store" Alias="Self" 
        Provider="System.Data.SqlClient" ProviderManifestToken="2005" 
       xmlns:store="https://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" 
       xmlns="https://schemas.microsoft.com/ado/2009/02/edm/ssdl">
  <EntityContainer Name="SchoolModelStoreContainer">
   <EntitySet Name="Course" EntityType="SchoolModel.Store.Course" 
              store:Type="Tables" Schema="dbo" />
   <EntitySet Name="Department" 
              EntityType="SchoolModel.Store.Department" 
              store:Type="Tables" Schema="dbo" />

    <AssociationSet Name="FK_Course_Department" 
                  Association="SchoolModel.Store.FK_Course_Department">
            <End Role="Department" EntitySet="Department" />
            <End Role="Course" EntitySet="Course" />
    </AssociationSet>

  </EntityContainer>

  <EntityType Name="Course">
       <Key>
         <PropertyRef Name="CourseID" />
       </Key>
       <Property Name="CourseID" Type="int" Nullable="false" />
       <Property Name="Title" Type="nvarchar" Nullable="false" 
                 MaxLength="100" />
       <Property Name="Credits" Type="int" Nullable="false" />
       <Property Name="DepartmentID" Type="int" Nullable="false" />
  </EntityType>

  <EntityType Name="Department">
       <Key>
         <PropertyRef Name="DepartmentID" />
       </Key>
       <Property Name="DepartmentID" Type="int" Nullable="false" />
       <Property Name="Name" Type="nvarchar" Nullable="false" 
                 MaxLength="50" />
       <Property Name="Budget" Type="money" Nullable="false" />
       <Property Name="StartDate" Type="datetime" Nullable="false" />
       <Property Name="Administrator" Type="int" />
  </EntityType>

  <Association Name="FK_Course_Department">
       <End Role="Department" Type="SchoolModel.Store.Department" 
            Multiplicity="1" />
       <End Role="Course" Type="SchoolModel.Store.Course" 
            Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="Department">
              <PropertyRef Name="DepartmentID" />
            </Principal>
            <Dependent Role="Course">
              <PropertyRef Name="DepartmentID" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        
</Schema>

The Mapping Specification

A mapping specification uses mapping specification language (MSL) to map the conceptual model to the storage model. This MSL fragment demonstrates a one-to-one mapping between the conceptual and storage models for the Course and Department entities in the School model.

<Mapping Space="C-S" 
          xmlns="https://schemas.microsoft.com/ado/2008/09/mapping/cs">
  <EntityContainerMapping 
                 StorageEntityContainer="SchoolModelStoreContainer" 
                 CdmEntityContainer="SchoolEntities">
    <EntitySetMapping Name="Courses">
     <EntityTypeMapping TypeName="SchoolModel.Course">
      <MappingFragment StoreEntitySet="Course">
       <ScalarProperty Name="CourseID" ColumnName="CourseID" />
       <ScalarProperty Name="Title" ColumnName="Title" />
       <ScalarProperty Name="Credits" ColumnName="Credits" />
       <ScalarProperty Name="DepartmentID" ColumnName="DepartmentID" />
      </MappingFragment>
     </EntityTypeMapping>
    </EntitySetMapping>
    <EntitySetMapping Name="Departments">
     <EntityTypeMapping TypeName="SchoolModel.Department">
      <MappingFragment StoreEntitySet="Department">
       <ScalarProperty Name="DepartmentID" ColumnName="DepartmentID" />
       <ScalarProperty Name="Name" ColumnName="Name" />
       <ScalarProperty Name="Budget" ColumnName="Budget" />
       <ScalarProperty Name="StartDate" ColumnName="StartDate" />
       <ScalarProperty Name="Administrator" 
                       ColumnName="Administrator" />
      </MappingFragment>
     </EntityTypeMapping>
    </EntitySetMapping>
   </EntityContainerMapping>
</Mapping>

Discussion

The School model that is discussed here uses a simple one-to-one mapping between a conceptual entity and a database table, but the Entity Framework supports more complex mappings such as table-per-hierarchy mapping or table-per-type mapping. For more information, see Entity Data Model Tools Scenarios and Defining Advanced Data Models.

See Also

Concepts

Entity Framework Terminology

Other Resources

Getting Started (Entity Framework)
Defining Advanced Data Models
CSDL, SSDL, and MSL Specifications