Implementing Entities (EDM)
In all Entity Data Model (EDM) applications, entities are defined in conceptual schema definition language (CSDL) and mapped to metadata that describes the data structures in store schema definition language (SSDL). This example demonstrates the design of a single entity and how it is mapped to storage.
The application for which this example entity is designed defines information about employees in a business enterprise.
Conceptual Schema
The entity defined in the following conceptual schema definition language (CSDL) syntax contains the specification for a data type in the Namespace named Employees
. The Namespace contains a single EntityType declaration: Employees
.
The properties of the Employees
entity contain basic information identifying an employee. The EmployeeId
property is declared as a Guid that uniquely identifies an instance of the type. The EmployeeId
property is tagged as the Key.
Three more properties identify the employee by FirstName
, LastName
, and Email
address.
In addition to the entity specification, a CSDL schema contains EntitySet and EntityContainer declarations. The EntityContainer and EntitySet specifications define the scope of data types in the data model. The EntityContainer contains the EntitySet. In this example, an EntitySet named Employees
contains the Employee
type.
<?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="Employees" Alias="Self" xmlns="https://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="EmployeesContext">
<EntitySet Name="Employees" EntityType="Employees.Employees" />
</EntityContainer>
<EntityType Name="Employees">
<Key>
<PropertyRef Name="EmployeeId" />
</Key>
<Property Name="EmployeeId" Type="Guid" Nullable="false" />
<Property Name="LastName" Type="String" Nullable="false" />
<Property Name="FirstName" Type="String" Nullable="false" />
<Property Name="Email" Type="String" Nullable="false" />
</EntityType>
</Schema>
The Namespace schema contains the Employees
entity and all the sets except the EntityContainer, which is separate and independent of the schema Namespace. For more information about the EntityContainer, see EntityContainerMapping Element (MSL).
Applications built on this data model reference the Namespace to use the class built on this model.
This CSDL specification is called the conceptual or design schema. Parallel entity names in the storage metadata in SSDL syntax correspond to storage structures. The EntitySet in this CSDL schema corresponds to the name of a table specified in the SSDL schema. The table contains data for instances of the Employee
entity used by applications built on the model. Properties of the Employee
entity correspond to the columns of the table.
Store Schema
The storage metadata in SSDL describes the database that contains instances of the Employees
defined in the conceptual schema.
The Employees.Store
namespace defined in the following schema is the target database of the model Employees
that has been defined in the conceptual schema.
The EntitySet Employees
in this schema represents a data table named Employees
. The EntityType specified by this schema corresponds to the EntityType defined in the conceptual schema. The name in the storage metadata does not have to match the name in the conceptual schema, but it does have to match the name of the database table.
The following schema describes a database object dbo
represented by the EntityContainer specified in the SSDL file. The EntityContainer in this example contains a table named Employees
.
?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="Employees.Store" Alias="Self"
Provider="System.Data.SqlClient"
ProviderManifestToken="2005"
xmlns="https://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="dbo">
<EntitySet Name="Employees" EntityType="Employees.Store.Employees" />
</EntityContainer>
<EntityType Name="Employees">
<Key>
<PropertyRef Name="EmployeeId" />
</Key>
<Property Name="EmployeeId" Type="uniqueidentifier" Nullable="false" />
<Property Name="LastName" Type="nvarchar" Nullable="false" MaxLength="50" />
<Property Name="FirstName" Type="nvarchar" Nullable="false" />
<Property Name="Email" Type="nvarchar" Nullable="false" />
</EntityType>
</Schema>
Mapping Specification
The mapping specification is defined in mapping specification language (MSL). This MSL specification connects the CSDL schema to the database using metadata in the SSDL schema.
The elements of this MSL schema include the EntityContainerMapping, the StorageEntityContainer, the EntityTypeMapping, a MappingFragment, and ScalarProperty mappings. The EntityContainerMapping maps a CdmEntityContainer to a StorageEntityContainer, in this case, the EmployeeSkills
entity container to the database object dbo
.
Note that names assigned to the StorageEntityContainer and CdmEntityContainer are not qualified by schema namespaces. StorageEntityContainer and CdmEntityContainer are separate and independent of the schema element in both the conceptual schema and the storage metadata. The use of a namespace name to qualify a StorageEntityContainer and CdmEntityContainer name will result in a mapping exception.
Following the StorageEntityContainer and CdmEntityContainer mapping, the EntitySetMapping specifies the StoreEntitySet that corresponds to the conceptual
TypeName, Employees
.
Properties of the TypeName are mapped by ScalarProperty elements as Name attributes corresponding to a ColumnName elements, for example, Name="EmployeeId" ColumnName="EmployeeId"
. The syntax Name specifies a property of an entity in the conceptual schema, and ColumnName specifies a column of the Employees
data table in the target database.
<?xml version="1.0" encoding="utf-8"?>
<Mapping Space="C-S"
xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
<EntityContainerMapping StorageEntityContainer="dbo"
CdmEntityContainer="EmployeesContext">
<EntitySetMapping Name="Employees" StoreEntitySet="Employees"
TypeName="Employees.Employees">
<ScalarProperty Name="EmployeeId" ColumnName="EmployeeId" />
<ScalarProperty Name="LastName" ColumnName="LastName" />
<ScalarProperty Name="FirstName" ColumnName="FirstName" />
<ScalarProperty Name="Email" ColumnName="Email" />
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
See Also
Concepts
Implementing Associations (EDM)
Other Resources
Schemas and Mapping Specification (Entity Framework)
EDM Specifications