Compartir a través de


Mapping a Conceptual Model to a Storage Schema

The Entity Framework provides an object-centric view of tabular data, expressed as entity types. An application developer only has to think about programming against the object model that is generated from the conceptual model, rather than having to also think about the database schema and how to access database objects and transform them into programming objects. The Entity Framework uses model schemas and mappings 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 EDM Generator (EdmGen.exe) tool.

The Conceptual Model

The conceptual model is an Entity Data Model (EDM) schema that defines the entities and associations in the EDM. The XML syntax that defines this model is called the conceptual schema definition language (CSDL). 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. Additional properties 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 conceptual model for the School EDM that defines the Course and Department entity types that are related by the FK_Course_Department association, with other entities and associations removed.

<?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="SchoolModel" Alias="Self" 
  xmlns="https://schemas.microsoft.com/ado/2006/04/edm">
  <EntityContainer Name="SchoolEntities">
    <EntitySet Name="Course" EntityType="SchoolModel.Course" />
    <EntitySet Name="Department" EntityType="SchoolModel.Department" />
    ...
    <AssociationSet Name="FK_Course_Department" 
      Association="SchoolModel.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="Int32" Nullable="false" />
    <Property Name="Title" Type="String" Nullable="false" 
      MaxLength="100" Unicode="true" FixedLength="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" />
    ...
    <NavigationProperty Name="Course" 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="*" />
  </Association>
  ...
</Schema>

The Storage Model

A separate data model uses store schema definition language (SSDL) to describe the logical model for persistent data, usually stored in a relational database. The data types of properties declared in SSDL files 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.

<?xml version="1.0" encoding="utf-8"?>
<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/2006/04/edm/ssdl">
  <EntityContainer Name="dbo">
    <EntitySet Name="Course" EntityType="SchoolModel.Store.Course" 
      store:Type="Tables" /> 
    <EntitySet Name="Department" 
      EntityType="SchoolModel.Store.Department" store:Type="Tables" />
     ...
    <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" />
    ...
  </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" />
    ...
  </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="CourseID" />
      </Dependent>
    </ReferentialConstraint>
  </Association>
  ...
</Schema>

The Mapping Specification

A mapping specification uses mapping specification language (MSL) to connect the types declared in the conceptual model to the database metadata declared in 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.

<?xml version="1.0" encoding="utf-8"?>
<Mapping Space="C-S" 
  xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
  <EntityContainerMapping StorageEntityContainer="dbo" 
    CdmEntityContainer="SchoolEntities">
    <EntitySetMapping Name="Course">
      <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Course)">
        <MappingFragment StoreEntitySet="Course">
          <ScalarProperty Name="CourseID" ColumnName="CourseID" />
          <ScalarProperty Name="Title" ColumnName="Title" />
          ...
        </MappingFragment>
      </EntityTypeMapping>
    </EntitySetMapping>
    <EntitySetMapping Name="Department">
      <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Department)">
        <MappingFragment StoreEntitySet="Department">
         <ScalarProperty Name="DepartmentID" ColumnName="DepartmentID" />
          <ScalarProperty Name="Name" ColumnName="Name" />
          ...
        </MappingFragment>
      </EntityTypeMapping>
    </EntitySetMapping>
    ...
    <AssociationSetMapping Name="FK_Course_Department" 
      TypeName="SchoolModel.FK_Course_Department" 
      StoreEntitySet="Course">
      <EndProperty Name="Department">
        <ScalarProperty Name="DepartmentID" ColumnName="DepartmentID" />
      </EndProperty>
      <EndProperty Name="Course">
        <ScalarProperty Name="CourseID" ColumnName="CourseID" />
      </EndProperty>
      <Condition ColumnName="DepartmentID" IsNull="false" />
    </AssociationSetMapping>
    ...
  </cs: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. For example, the relational database might use more than one table to store data related to each employee. One table could contain contact information for all people and a separate related table could contain information that pertains only to employees. The Entity Framework enables developers to define entities using inheritance hierarchies and to map one entity to data from more than one table in a database, with full support for data updates, inserts, and deletes. For more information, see Data Modeling in the Entity Framework.

See Also

Concepts

Entity Framework Terminology

Other Resources

Getting Started (Entity Framework)
EDM Specifications
Defining Advanced Data Models (Entity Framework Tasks)
Schemas and Mapping Specification (Entity Framework)