Mapping 101: Part 1

 

Now that you’re getting accustomed to the Entity Framework and Entity Data Model (Entity Data Model 101: Part 1 and Entity Data Model 101: Part 2), we’ll now take a look at the Object Relational Mapping (ORM) characteristics of the Entity Framework. We’ll post several “Mapping 101” topics to describe the content of the artifacts required by the Entity Framework for supporting mappings between Entity Models and an underlying database. We expect the content of these artifacts to be best edited with our tool support in Visual Studio, but until those are ready, users may want an understanding of these artifacts in detail.

Note: These Mapping 101 posts will make use of the “SampleQueries” solution that ships with the February CTP and its underlying model to best help illustrate the content of these artifacts.

These Mapping 101 posts will progress along in complexity with the EDM 101 posts. In the first EDM 101 post, we described the CSDL file, or Conceptual Schema Definition Language to define the Entity Model. In this post, we’ll describe the basics of mapping that artifact to a database. To do that, we need to introduce two additional types of artifacts that support the Entity Framework:

· Store Schema Definition Language (SSDL) artifacts: Describes the structure of the underlying database, including tables, columns and functions used to support the Entity Model.

· Mapping Schema Language (MSL) artifacts: Describes the mappings between an Entity Model and a Store Model.

One point to make clearly early on in our mapping lesson is that, even though a user can define multiple artifacts for their models and mappings, an “Entity Container” defined in an Entity model can be mapped in a single MSL file. This is because we need to know the entire scope of the mapping of a container while we’re loading the artifacts.

SSDL Files

In similar manner to how CSDL files define a model using Containers, EntityTypes and EntitySets, SSDL files define a database model the same way. The key portions of an SSDL file include:

· The Namespace: In SSDL files, the namespace describes the database under the Entity Model.

· The EntityContainer definition: In the case of an SSDL file, the container helps define the schema in which an Entity Set lives.

· EntityType definitions: Types define the structure of a table. These Entity Types can be re-used when defining EntitySets if several tables have the same structure.

· EntitySet definitions: EntitySets define instances of tables in the database. There’s a 1-1 relationship between an EntitySet and a table in the database.

· Association definitions: Associations are Association Types used to define relationships between Entity Types (Tables). In the EDM, we use Associations to define the Foreign Key structure between tables

· AssocationSet definitions: Defines instances of relationships between Entity Sets

Here’s a sample of a streamlined version of the SSDL file from the SampleQueries project:

<?xml version="1.0" encoding="utf-8"?>

<Schema Namespace="Northwind" Alias="Self" xmlns="https://schemas.microsoft.com/ado/2006/04/edm/ssdl">

  <EntityContainer Name="dbo">

    <EntitySet Name="Categories" EntityType="Northwind.Categories" />

    <EntitySet Name="Products" EntityType="Northwind.Products" />

    <AssociationSet Name="FK_Products_Categories" Association="Northwind.FK_Products_Categories">

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

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

    </AssociationSet>

   

  </EntityContainer>

 

  <EntityType Name="Categories" Key="CategoryID">

    <Property Name="CategoryID" Type="int" Nullable="false" StoreGeneratedPattern="identity" />

    <Property Name="CategoryName" Type="nvarchar" Nullable="false" />

  </EntityType>