Szymon Kobalczyk's Blog

A Developer's Notebook

  Home  |   Contact  |   Syndication    |   Login
  106 Posts | 6 Stories | 576 Comments | 365 Trackbacks

News

View Szymon Kobalczyk's profile on LinkedIn

Twitter












Tag Cloud


Article Categories

Archives

Post Categories

Blogs I Read

Tools I Use

Two months ago, after listening to .NET Rocks! show with Daniel Simmons, I was really curious to learn more about ADO.NET Entity Framework (EF). As part of my learning I was ready to write a tutorial on how to get started with this framework. However there were no tools available at the time and with instructions on how to manually edit XML files the article turned very long, and quite frankly looked very boring. Hence I decided to postpone publishing it and wait for the tools to came out.

This week Microsoft released ADO.NET Entity Framework Beta 2. You can find the complete release notes on the ADO.NET Team Blog so I won’t repeat it here. What’s more important we finally get the first CTP of Entity Framework Tools. So now I have all the pieces to attempt to write my tutorial again.

• • •

Microsoft went a long road to get into the ORM space starting with ObjectSpaces, then WinFS and now LINQ and Entity Framework. For those interested in the full story here is nice article from one of the authors. Even though I'm by no means an ORM expert, I've worked before with number of both commercial and open source systems falling into this category.

According to ADO.NET team Entity Framework is much more then just another ORM tool. The aim here is to solve a more generic problem of mapping conceptual model to application logic which is prevalent not only in storing programming objects but also in such areas as data replication, remoting and reporting. Which EF you maintain a single conceptual model of your domain objects (called entities here) that can be later mapped and reused with other representation.

To learn new technology I usually develop a sample project so I have some well defined goal to achieve. This time again I choose to work with the PetShop 4.0 sample application. I prefer this sample for several reasons:

  1. It contains existing entity model, so nice thing to try would if the whole data access code can be replaced with EF.
  2. The application uses four databases in total, so it would test how to map the entity model to multiple data sources.
  3. It is relatively simple and I worked with it before when learning CAB.

Besides Northwind and Adventure Works are already mapped as EF samples so it would be no fun in using them.

Step 1. Generating entity model from database

Working with Entity Framework involves creating three schemas for your data:

1. Conceptual Schema (CSDL) — represents the conceptual model of your data, that is entities and their relationships (in current version this maps 1:1 to the generated object model). 

2. Storage Metadata Schema (SSDL) — describes the representation of your data when its stored in the database (or other storage media).

3. Mapping Specification (MSL) — as you can already guess this one describes how to map data from one of the above schemas to the another.

All three are now defined as part of single XML document that will be part of your project (edmx).

The normal workflow for Entity Framework should start with first creating the concept model of entities and then work the storage and mapping schemas from there. However in many cases you will be working with existing applications, so it would be more convenient to start from the database schema instead. This release of EF targets this "data first" scenario with the EDM Wizard built in Visual Studio.

As I said before PetShop uses four databases: product inventory, orders, and two for ASP.NET users and profiles. For now I will concentrate on the first one. So to begin with let's see how this database looks like:

PetShop 4 Inventory Database Diagram

As you see this database is extremely simple: each category has multiple products, that in turn hold multiple items. Each product item points to its supplier and there is additional table that holds current inventory count.

Okay, I'm ready to create the entity model. First I've created a new C# class library project, and then from the Add New Item dialog selected the ADO.NET Entity Data Model template. This starts the Entity Data Model Wizard where in first step we select either to generate model from database or to create empty one:

 EDM Wizard: Step 1

After selecting the first option we go to the next step when we need to specify the connection string to our database:

EDM Wizard: Step 2

Note that the connection string used by the EF besides the typical settings for ADO.NET also contains paths to all three schema files.  

In the last step we can select which tables, views and stored procedures should be included in the generated model (in this case I want to include all tables besides the AspNet_SqlCacheTablesForChangeNotification). We can also specify the namespace for the generated classes:

EDM Wizard: Step 3

After pressing the Finish button the wizard generates the entity model (the PetShopModel.edmx file) and adds all required references to the project. In the previous version the wizard didn't worked very well and you'd had to use the command line tool called EDM Generator (edmgen.exe) to get these files. This time things changed for good so lets look at what was generated.

When we open this file from the Solution Explorer we can finally see our enities inside the EDM Designer (click on image to enlarge):

EDM Designer

Notice that although this model looks very similar to the database model shown earlier it actually shows entities not tables. So besides the scalar properties that are mapped to the table columns we also have the navigational properties mapped to table relationships. There is also a new tab next to the Solution Explorer called Entity Model Browser that displays the structure of the model. We can see here that this model includes both the conceptual (entity) and storage schema. Finally at the bottom we see new window titled Entity Mapping Details that displays how the two schemas are mapped.

In the second part I will look at how we can modify the generated model to align it with our concept model and at the same time learn how to use this designer. Stay tuned!

posted on Wednesday, August 29, 2007 8:30 PM

Feedback

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 1/13/2008 3:53 AM Richard Fencel
Thanks for your illustrative and informative web site. I love pictorial explanatiosn. Unfortunately, almost nobody on the internet mentions one little thing: the Entity Model Wizard is NOT part of Visual Studio 2008 and must be installed in three parts:

Entity runtime
Entity patch
Entity tools

I finally founda website that explains how to install:

http://oakleafblog.blogspot.com/2007/12/entity-framework-beta-3-available-for.html

Regards,

Richard Fencel

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 4/22/2008 10:36 PM Ryan
Did you ever do a part 2?

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 7/7/2008 5:23 AM oyen
where's part 2 sir?

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 7/29/2008 11:44 AM Ervin
Thanks for the pictures screen shot. It save my time to install whole stuff just to view how it look like. Thanks

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 11/7/2008 7:43 AM eguana erwin c.

i want to learn on how to make a mini data base, together with the ERD design, and the conceptual schema....

please displayed if you have post your responce....



# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 1/17/2009 1:25 PM Nitin
A real nice kick start with example. Kudos to you sir.

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 1/21/2009 10:24 AM WAB69
It has been over a year, what about part two?

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 7/14/2009 5:22 AM newbi
thanks for the tutorial. but where is the part 2? its so useful that i need to see the next part

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 8/25/2009 10:42 PM CPCooper
Great Part I...... waiting for Part II.

However I do have a question, in the CSDL Item Table should Supplier be SupplierId? If not, how is the Supplier information captured?

Thanks

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 12/4/2009 3:49 PM Nelson
You show the easy part. We're waiting for Part II where you (hopefully) will show us how to flatten the model. What you showed here anyone can figure out for themselves!

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 6/22/2010 3:01 PM Alan
nice! do the second part! :D

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 6/10/2011 4:15 PM aaaaaaaa
ascsdcsc ddvdvasvd

#  Getting started with ADO.NET Entity Framework. Part I: Mapping entities 7/13/2011 11:31 AM Muhammad Amin
Nice article

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 11/29/2011 8:14 AM Yuniar
This is the first time I use EF. Your article very helpfull. Thanks

# re: Getting started with ADO.NET Entity Framework. Part I: Mapping entities 12/3/2011 12:14 AM Himanshu
Thanks a lot for specifying info about
1. Conceptual Schema (CSDL)
2. Storage Metadata Schema (SSDL)
3. Mapping Specification (MSL)

Looking the next page to this article.

Post A Comment
Title:
Name:
Email:
Comment:
Verification: