Entity Framework 4.0 – Part1: How to create a model from a database

These tutorials are built using Visual Studio 2010 RC and .NET 4.0. RC. This means that Entity Framework 4.0 is used. At the moment of writing this, VS2010 and .NET 4.0 are not yet released as RTM so information provided in this post could change in the future.

In this series of blog posts I’ll explain to you how to work with Microsoft’s ORM, Entity Framework. Version 4.0 is used during the tutorials. I didn’t really blog that much before on Entity Framework because I thought the concept and usage was not ready. Now with the new release EF, I feel much more comfortable evangelizing it. Following, I’ll explain to you how you can create an entity framework model based on an already existing database.

Let’s start by creating a new console application and add a new file to the project. When the “Add new item window” comes up, select ADO.NET Entity Data Model.

image

Now the wizard will start. This wizard will help you to build up your entity model. Here you have two options:

  1. Generate from database: Here the model will be generated based on an existing database with tables.
  2. Empty model: Before Entity Framework 4.0 this didn’t really do much. But now this has changed a lot! From here you can create your own model (using the designer) and even generate a database script (to create the database and tables). How you can start from an empty Entity Framework model will be discussed in an upcoming post.

image

So we chose to generate the model from the database. This means we have select our database and tables. The wizard now asks us to select the database.

I’m going to create a “New Connection…” because I haven’t used this database before.

image

This dialog window isn’t new. If you have worked before with databases in Visual Studio, you should already be familiar with this window. It just asks you to select a server name and a database on that server.

image

Once the database has been selected, the wizard will generate the Entity connection string. This is something like a regular database connection string. The only difference is that it contains more information on the Entity Framework model. Make sure that you check “Save entity connection string in App.Config”. This will save the connection string to an App.Config file.

image

Next we need to select the tables where we want to generate entities for. In my demo I’m only going to work with the Article table. Notice that you can also add database views or stored procedures to your entity model.
Make sure that you check the two checkboxes on this window.

Pluralisation is something new in EF4.0. If you check this box, EF4.0 will pluralize the name of you entity sets based on the Entity name. This will make querying a lot more readable in your code. Before when you had an entity that called Article, you entity set would also be called Article. Now when your entity is called Article, the entity set will be called Articles. The pluralisation mechanism in Visual Studio is actually quite smart! It doesn’t just add an ‘’s’ at the end of the entity name. For instance: Category will pluralize to Categories and Ox will even be pluralized to Oxen. How neat is that? For now the pluralisation only works correctly for the English language. No problem though, you can easily create you own pluralisation service where you can add your own words. Dan Rigsby has a nice blog post on this: Entity Framework 4.0: Pluralization

image 

Once the model is completely generated, you will see that Visual Studio has created an .edmx file (the entity model) together with its generated code behind class and an App.Config (where we stored the entity connection string).

image

If you click on the .edmx file, you will see a diagram of the model.

image

The WPF text editor in Visual Studio 2010 allows us to zoom the model in and out via the scroll wheel on the mouse. If you have a lot of entities in your model, you can easily get a good overview of the diagram. Under the scrollbar you have some different options to alter the view of the diagram.

From top to bottom

  • Zoom in on the diagram
  • Zoom the diagram to 100%
  • Zoom out on the diagram
  • Launch the thumbnail viewer: This allows you to better navigate over the complete diagram

image 

If you select an entity and click on the mapping details you will see how the table fields are mapped to the properties of the entity. If you want you can change details in the properties window. Information that was set on the table fields is also set on the entity properties. For example if you create a field in the table with a max length of 100 (varchar(100)) this will also be set in the entity property. This is not something that you will find back in the generated code behind class of the model. It’s actually stored in the conceptual model.

image

image 

Now let’s write some code to retrieve data from the database. As you can see, you only need to create a new instance of you entity model. Then you can retrieve your records by calling the entity set of you entity. See how easy this is?

class Program
    {
        static void Main(string[] args)
        {
            using (var dbentities = new DemoDBEntities())
            {
                foreach (var article in dbentities.Articles)
                {
                    Console.WriteLine(string.Format("Name: {0}",article.Name));
                    Console.WriteLine(string.Format("Description: {0}",article.Description));
                    Console.WriteLine(string.Format("Price: {0}",article.Price));
                    Console.WriteLine(string.Format("Stock: {0}",article.Stock));
                    Console.WriteLine();
                }
            }
            Console.ReadLine();
        }
    }

image

Hope this was informative!

If you have any questions or remarks, please let me know..

kick it on DotNetKicks.com


No comments yet. Be the first.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word