Creating an Entity Data Model (Entity Framework 4.0)


What is Entity Framework?

The ADO.NET Entity Framework is a new data access platform from Microsoft for writing .NET applications. It is a set of technologies in ADO.NET that helps fill in the space between object-oriented development (objects) and databases. Basically it is an Object-Relational Mapping (ORM) method of mapping relational databases and object-oriented programming languages implemented using .Net.

With Entity Framework 1.0 (the previous version of EF) you could only generate a model from an existing database, and you could also start with an empty model and create the conceptual model from scratch but you could not create the database from that model. Using Entity Framework 4.0 you can create an empty EDM (Entity Data Model) and then generate the database, mappings, and classes from the defined model. And it allows you to use the Entity Framework using Plain Old CLR Objects (POCO) entities without an EDMX file.

Database-First Approach

Open Visual Studio 2010 and create a new web site. Make sure on the New Web Site dialog that you have the .NET Framework 4.0 selected. And name it MyFirstEFProj.

EntDataModel1.gif

Now open solution explorer and right-click on the project. Select Add New Item from the context menu. This will open the Add New Item dialog. Select the ADO.NET Entity Data Model template and click Add.

EntDataModel2.gif

It will show you Entity Data Model Wizard. EDM Wizard has two options 1 > Generate from database and 2> Empty model. Select first option and hit next.

EntDataModel3.gif

In the next wizard you have to specify the data connection for your EDM. If you have created a connection previously it will show you the list. If you haven't you will need to create one. Click on the New Connection button.

EntDataModel4.gif

It will open the Connection Properties dialog. The Connection Properties dialog looks very similar to SQL Server connection dialogs. You need to provide the server name, authentication method (Windows for SQL Server authentication), and the database name and click the Test Connection button to make sure all the connection settings are correct. And hit OK on the Connection Properties dialog.

EntDataModel5.gif

It will take you back to the EDM connection wizard with your new connection. There are two radio buttons on this wizard. The two states that you will either include the username and password in the connection string or you will set the username and password in your code. One of the options should be enabled.

EntDataModel6.gif

The next wizard lets you to select the database objects to include in the EDM. Here you can select tables, views, and stored procedures.

EntDataModel7.gif

Make sure both checkboxes available on wizard are checked and hit finish.

EntDataModel8.gif

It will show you generated EDM model in the Designer window. You have just created your first Entity Data Model generated from an existing database.