Entity Data Model 101: Part 1

Published 30 January 07 01:42 PM | dpblogs 

 

Most developers are familiar with the Object/Relational Mapping (ORM) problem: databases use the abstraction of rows in tables, but application programs use the abstraction of classes and objects. Existing ORM frameworks tend to address this mismatch by allowing programming classes to be annotated in order to relate them to the database.

The intent with ADO.NET is more ambitious: We view the ORM problem as just one of a number of services we want to build on the database. Other services include reporting, synchronization, backup, and so on. In order to cover all of these services, we have designed a data model that is similar to the object-oriented idiom that programmers use, while remaining independent of any particular programming language or programming platform. This data model is the Entity Data Model (EDM).

The EDM is a conceptual model for designing the data layer of applications. Entities and relationships in the domain of an application are specified in XML syntax. Both the programming classes used in applications and the storage schema map to this conceptual model.

Entities and Associations

The EDM is an implementation of the entity-relationship (ER) model. An EDM entity type is a specification for something in the application domain that must be represented by data. Examples of entity types can be found in a typical line of business (LOB) application and might include employees, customers, orders, order-lines, suppliers, shippers, products, invoices, and so on.

Logical connections between entities are called relationships: for example, the logical association between an employee and his/her contact information or between a merchandise order and the customer who makes the order. An EDM relationship type describes the possible associations between entities, and any constraints on multiplicity.

Schemas

The EDM provides an XML syntax named conceptual schema definition language (CSDL). A CSDL schema is used to specify entities and relationships in the domain of an application.

 

The CSDL schema will become the core data model of the application. It is usually referred to simply as the “conceptual schema”. A CSDL schema declares and defines a Namespace that contains entities and associations. An Alias can be assigned to save typing in the syntax that will follow. A schema is declared using the following XML:

 

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

<Schema Namespace="AdventureWorksModel"

    Alias="AdventureWorksModel"

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

  

</Schema>

 

 

Entities

If the application requires a data type to represent an employee, the schematic representation of the employee type in CSDL looks something like this:

 

  <EntityType Name="Employee" Key="EmployeeID">

 

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

    <Property Name="LoginID" Type="String" Nullable="false"

        MaxLength="256" />

    <Property Name="Title" Type="String" Nullable="false"

        MaxLength="50" />

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

    <Property Name="HireDate" Type="DateTime"

        Nullable="false"/>

    <Property Name="SalariedFlag" Type="Boolean"

        Nullable="false" />

    <Property Name="VacationHours" Type="Int16"

        Nullable="false" />

    <Property Name="SickLeaveHours" Type="Int16"

        Nullable="false" />

    <Property Name="CurrentFlag" Type="Boolean"

        Nullable="false" />

  <!--Other properties-->

 

    <NavigationProperty Name="Contact"

        Relationship="AdventureWorksModel.FK_Employee_Contact_ContactID"

        FromRole="Employee" ToRole="Contact" />

 

    <NavigationProperty Name="Managed_Employees"

        Relationship="AdventureWorksModel.FK_Employee_Employee_ManagerID"

        FromRole="Employee" ToRole="ManagedEmployee" />

 

      </EntityType>

 

 

You don’t have to understand everything in this syntax to see that it declares an entity type named Employee and that it contains properties of various data types.

 

Properties of Entities

The first two lines of the example include declaration of the Name and Key attributes of the entity. When an instance of the Employee entity is created, the Key property is assigned an integer value that uniquely identifies the instance for all operations in which it is used.

 

The Key attribute can be any sequence of properties. Using an integer is only one possibility. The Key could also be a GUID or the combined sequence of one or more properties separated by a space in the attribute declaration.

 

Each property of the Employee type has a simple data type and several facets that further constrain the data that can be assigned to the property. For example, the Employee BirthDate property is of type DateTime. It has one designated facet: Nullable=”false”. The Nullable facet specifies that the BirthDate property cannot be null. The Employee entity also contains a LoginID property. This property is of type String. The facets of the String data include Nullable=”false” and MaxLength=”256”.

 

Attributes declared as Type=”x” are EDM types, each of which has a corresponding CLR type and corresponding SQL type. (The actual SQL type depends on the underlying database.)

 

The properties of this Employee entity type align with an Employee table in the Adventureworks sample database that ships with Microsoft® SQL Server™ 2005. Using the schemas in this article, you can build a class library mapped to data in Adventureworks. This class library would contain an Employee class to represent the Employee entity type.

 

The last six lines of the CSDL syntax shown above declare two navigation properties on the Employee type. We’ll return to these later.

 

To make our example more interesting, we also declare a Contact entity type.

Contact entities contain properties that will be assigned the various data needed to identify and locate a person or business.

 

  <EntityType Name="Contact" Key="ContactID">

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

    <Property Name="NameStyle" Type="Boolean" Nullable="false" />

    <Property Name="Title" Type="String" MaxLength="8" />

    <Property Name="FirstName" Type="String" Nullable="false"

         MaxLength="50" />

    <Property Name="MiddleName" Type="String" MaxLength="50" />

    <Property Name="LastName" Type="String" Nullable="false"

         MaxLength="50" />

    <Property Name="Suffix" Type="String" MaxLength="10" />

    <Property Name="EmailAddress" Type="String" MaxLength="50" />

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

    <Property Name="Phone" Type="String" MaxLength="25" />

    <Property Name="PasswordHash" Type="String" Nullable="false"

         MaxLength="128" />

    <Property Name="PasswordSalt" Type="String" Nullable="false"

         MaxLength="10" />

    <Property Name="rowguid" Type="Guid" Nullable="false" />

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

  </EntityType>

 

 

Associations and Navigation Properties

 

An Association between Employee and Contact entities is declared and defined in the following CSDL syntax.

 

  <Association Name="FK_Employee_Contact_ContactID">

    <End Role="Contact" Type="AdventureWorksModel.Contact"

        Multiplicity="1..1" />

    <End Role="Employee" Type="AdventureWorksModel.Employee"

        Multiplicity="1..*" />

  </Association>

 

 

Recall that the definition of the Employee entity includes the NavigationProperty between the Employee entity and the Contact entity. This NavigationProperty makes it possible to follow an instance of an Employee entity according to the Association above to an instance of a related Contact entity or vice versa. The Association above declares the End elements of the Association and defines Role, Type, and Multiplicity attributes of these End elements.

 

The Multiplicity attributes of the End elements of the Association specify the number of instances of each End element that can participate in the Association. In the Association named FK_Employee_Contact_ContactID, one Employee can be related to any number of Contact instances; that is, the Multiplicity of the Employee End is “1..*” and the Contact End is “1”.

 

The attributes of the End elements specify a logical Role in the relationship and an explicit type for each End.

 

Type names are qualified by the name of the schema Namespace in which they are declared. In the example above, the schema namespace name is AdventureworksModel.

 

The FK_ prefix on the Association name follows a convention that suggests the corresponding foreign key constraint in the database that exists between an instance of the Employee entity and an instance of the Contact entity. This convention is not enforced by the EDM.

 

The second NavigationProperty in the Employee entity uses an Association in which both End elements are Employee entities. This Association is declared in the following CSDL syntax.

 

<Association Name="FK_Employee_Employee_ManagerID">

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

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

  </Association>

 

 

Note that the Type attributes of both End elements of this Association are specified as type AdventureWorksModel.Employee. The NavigationProperty is named Managed_Employees to indicate the manager/managed employees relationship between instances of the Employee entity. In this schema relationship, an employee can manage any number of other employees.

 

This concludes the introduction to fundamentals of CSDL syntax used in declarations of entity and association types. In part 2 of this discussion we’ll explore the logical groupings of entities and associations called entity sets and association sets that map to tables in storage.

 

Mike Dodaro

Programming Writer for ADO.NET

 

Entity Data Model 101 - Part 2

 

Entity Data Model 101 - Part 3

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Marcelo's WebLog said on February 2, 2007 12:33 PM:

You can find the first part of what promises to be a very educational series over here: http://blogs.msdn.com/adonet/archive/2007/01/30/entity-data-model-part-1.aspx

# system.data.objects dev guy said on February 2, 2007 3:14 PM:

If you are interested in ADO.Net Orcas and the Entity Framework, then you should check out this new posting

# Roger Jennings said on February 2, 2007 4:31 PM:

Mike Dodaro, a Microsoft programming writer for ADO.NET, has published Part I of what promises to be an interesting series of technical articles about the forthcoming ADO.NET 3.0 Entity Data Model (EDM). ...

New Entity Data Model 101 Documentation Series

http://oakleafblog.blogspot.com/2007/02/new-entity-data-model-101-documentation.html

# Jason said on February 4, 2007 9:46 PM:

Does anybody know of an expected release date for the entit framework?  I'm eagerly anticipating it and trying to determine if an application I am developing can wait for this to be released.

Thanks.

# John Papa [MVP C#] said on February 9, 2007 10:55 AM:

Here are a few good links to Orcas material (some old and some new). There is a lot of good and bad material

# Sam Gentile said on February 11, 2007 5:00 PM:

Winter has finally set in with single digit temps and minus degrees wind chills but still no snow. WPF/Avalon

# Guy Burstein's Blog said on February 12, 2007 1:22 PM:

The ADO.Net team has published a serries of posts about the Entity Data Model (EDM) which is part of

# Writing about Programming, Programming about Writing said on February 15, 2007 12:44 AM:

Here are a few interesting blog posts. Eric White's Functional Programming Tutorial is a few months old

# elisaj said on February 15, 2007 4:52 PM:

Jason,

At the moment, we do not have an expected release date for the Entity Framework. We do however expect it to ship with the Visual Studio "Orcas" release in the second half of the year.

# ADO.NET team blog said on February 26, 2007 2:39 PM:

Now that you’re getting accustomed to the Entity Framework and Entity Data Model ( Entity Data Model

# ADO.NET team blog said on May 10, 2007 4:38 PM:

Continuing along with posts on the essentials of Entity Framework mapping (see Mapping 101: Part 1 ),

# ADO.NET, LINQ, DataSet and other fun .NET technology said on July 31, 2007 8:14 PM:

I was recently asked what a high level list of good starting points for the Entity Framework is. This

# Mike Taulty's Blog said on August 9, 2007 7:55 PM:

Note: Some of the posts referenced in this post (!) are likely to be more out of date than others but...

# ADO.NET team blog said on September 5, 2007 8:25 PM:

There are two new data access technologies coming out of Microsoft in the coming year that have particular

# Ronan Geraghty's Weblog said on September 11, 2007 11:06 AM:

So far for me LINQ to SQL has been perfectly adequate and has just worked without any intervention needed.However,

# MSDN Ireland Blog said on September 11, 2007 11:06 AM:

So far for me LINQ to SQL has been perfectly adequate and has just worked without any intervention needed.However,

# BonzBlog Michaela Juřka said on September 17, 2007 3:07 PM:

Poslední reinkarnace LINQu, o které jsem se zmínil v přehledovém článku , je LINQ to ADO.NET Entities,

# BonzBlog Michaela Juřka said on September 17, 2007 3:08 PM:

Poslední reinkarnace LINQu, o které jsem se zmínil v přehledovém článku , je LINQ to ADO.NET Entities,

# Cristian's Blog said on October 3, 2007 4:08 AM:

SQL Server 2008 is at the door and the same stands for Visual Studio 2008. The data access strategy is

# ctl00$_$ctl00$_$ctl01$_$form$_$tbname said on May 6, 2008 3:34 PM:

definition syclone Cyclone <a href= http://cyclone-definition.barerube.cn >definition 1959 Cyclone</a> [url=http://cyclone-definition.barerube.cn]definition 1959 Cyclone[/url]

# carlc said on May 15, 2008 12:42 AM:

Great info - however I've not been able to find any in-depth tutorials on the EDM. Does anyone have a link to some good source of info? Thanks!

# 247Blogging said on May 19, 2008 8:56 PM:

Most developers are familiar with the Object/Relational Mapping (ORM) problem: databases use the abstraction of rows in tables, but application programs use the abstraction of classes and objects. Existing ORM frameworks tend to address this mismatch

# ctl00$_$ctl00$_$ctl01$_$form$_$tbname said on June 6, 2008 5:26 AM:

sex zoo <a href= http://bikecobu.cn/index.html >ebony sex</a> [url=http://bikecobu.cn/index.html]ebony sex[/url]

# Abhishek Hingu said on September 22, 2008 6:31 AM:

Unable to see another kind of database provide while i am trying to create Entity model using VS2008 Designer

# alex said on July 21, 2009 3:30 AM:

Hi All,

Can anybody recommend good book on EDM

# alex said on July 21, 2009 3:30 AM:

Hi All,

Can anybody recommend good book on EDM.

Thanks,

Alex

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker