Szymon Kobalczyk's Blog

A Developer's Notebook

  Home  |   Contact  |   Syndication    |   Login
  84 Posts | 5 Stories | 164 Comments | 380 Trackbacks

News

View Szymon Kobalczyk's profile on LinkedIn

Twitter












Article Categories

Archives

Post Categories

Image Galleries

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

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 1 and 2 and type the answer here: