Create an Entity Data Model From a Database – Entity Framework 4.0
Posted by: Suprotim Agarwal ,
on 11/3/2010,
in
Category Entity Framework
Abstract: In this article, we will create our first Entity Framework application using Visual Studio 2010. We will create an Entity Data Model (EDM) from an existing database.
In my previous article, Entity Framework 4.0 FAQ – Getting Started Guide we looked at some of the most frequently asked questions on Entity Framework 4.0. Make sure you read it before you move ahead.
In this article, we will quickly jump to create our first Entity Framework application using Visual Studio 2010. Before that, I want to introduce you to a core component of the Entity Framework, the Entity Data Model (EDM). The EDM describes the structure of your Business objects, which includes the data types, relationship types, schema mapping and so on. In simple words, EDM is a bridge between your application and data store and lets you work at a conceptual level with your data, rather than the actual schema.
With Entity Framework 4.0, you can do the following:
• Generate a Model from the Database (Database First approach)
• Generate a Database from a Model (Model First approach – new to EF 4.0)
• Code-Generation using T4 Templates and POCO (Code First approach – new to EF 4.0)
Let us create an Entity Data Model (EDM) from an existing database, say Northwind. I assume you have either downloaded Visual Studio 2010 Ultimate Trial Edition or the free Visual Studio 2010 Express Edition. Let’s get started:
Step 1: Open Visual Studio 2010 > File > New Project. In the templates, select C# or VB, Windows > Console Application. Make sure the target framework is ‘.NET Framework 4.0’. I have called the application ‘EFConsole’. Click Ok.
Step 2: Right click on the ‘EFConsole’ project in Solution Explorer > Add > New Item. Select the Data Template > ADO.NET Entity Data Model and click Add.
Note: If you plan to use the same Entity Model in different projects, then make sure that you create your Entity Model as a separate class project. This way, you will be able to add its reference in multiple projects.

Step 3: The Entity Data Model Wizard appears. Choose ‘Generate From Database’ and click Next

You have to now choose your Data Connection. If you have created connections previously, then they will show up in the list. I assume you have not, so click on the ‘New Connection…’ button and set up the connection properties to the Northwind database, as shown below:

Click OK to return to the wizard and you will see that the new connection is visible in the data connection dropdown.

Our Entity Connection Settings will be stored in the App.Config file as ‘NorthwindEntities’. You can change the name to anything else you want. Click Next
Step 4: The next step is to choose the Database Objects you want to include in your Entity Data Model (EDM). To keep it simple, I am choosing two tables ‘Customers’ and ‘Orders’. Call the model namespace as ‘NorthwindModel’

The checkbox ‘Pluralize or singularize generated object names’ gives us the ability to pluralize or singularize object names. This is new to EF 4.0 and is a welcome change, as in EF 3.5, not being able to rename/pluralize objects in the model, caused a great deal of confusion.
Click Finish to complete the wizard.

Congratulations, our EDM is ready! You have just generated a Model defined by EDM, from the Northwind database. The screenshot shown above is the representation of your Entity Data Model in the designer.
Query against the Entity Data Model
If you recall, I had mentioned in my previous article Entity Framework 4.0 FAQ – Getting Started Guide that we will be programming against a logical model rather than a physical database. You can use ‘LINQ to Entities’ or ‘Entity SQL’ to write queries against your model defined by EDM. Entity Framework behind the scenes uses ADO.NET provider to translate these queries to what SQL Server understands and bring back the results.
Note: Since we are connecting to SQL Server, we will not need any additional providers to make a connection. If you plan to use Oracle, DB2, MySQL etc, make sure you look at the additional 3rd party providers for these databases.
In this step, I will show you how to query against the model you have just created. We will write code that displays the CustomerId and CompanyName of customers who have placed orders after the 1st of January, 1998.
Open Program.cs and write the following query in your Main method
static void Main(string[] args)
{
using(var context = new NorthwindEntities())
{
var custWithOrders = context.Orders
.Where(o => o.OrderDate > new DateTime(1998, 1, 1));
foreach (var cust in custWithOrders)
{
Console.WriteLine("{0} from {1} Ordered Item on {2}",
cust.Customer.CustomerID,
cust.Customer.CompanyName,
cust.OrderDate
);
}
Console.ReadLine();
}
}
The following output should be generated on your console window

There you go! You have written your first query against your model. I hope you now see how EF provides a shift from Database-oriented (DataReader, DataSet) to Model-oriented development. So instead of focusing on a Database, a developer starts focusing on the Entities that represents the Business Model of the application.
I know there are a lot of questions running in your mind about the query we just wrote. Just hold on to these queries for now. They will soon get cleared in the forthcoming articles.
Update: If you are curious to know how EDM generated the code and translated the queries to something that SQL Server understands, read my article Exploring how the Entity Data Model (EDM) Generates Code and Executes Queries – Entity Framework 4.0
I hope you liked this article and I thank you for viewing it.
The entire source code of this article can be download over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn