Entity Framework – How to Create an EDMX file in Visual Studio

Entity Framework – How to Create an EDMX file in Visual Studio

Microsoft has created Entity Framework (short form EF) for helping developers to automate database related operations in C#, VB, C++ and other programming languages. Due to EF developers are not needed to do the lengthy ADO.NET programming as EF takes all its care.

EF helps developers in many ways like:
  • 1. Less database related codes.
  • 2. Fast development of projects.
  • 3. Better transaction.
  • 4. Testability improvements.

Here in this tutorial I will help you to get stated with Entity Framework.

Install Entity Framework (EF) via Nuget

You can install Entity Framework through Nuget in Visual Studio. This procedure is same for all versions of Visual Studio and EF.

Right click on your Project in Solution Explorer and select “Manage Nuget Packages”.
install entity framework

This will open “NuGet Package Manager” window. Here in the search box enter “Entity Framework”. When EF shows up, select it and press the install button.
install entity framework through nuget

Next click “I Agree” button on the “License Acceptance” window. This will start the installation of EF.
license acceptance in EF

It takes less than a minute to complete and you will see “Successfully installed” message in the Output window.
EF installed successfully

Restore Database in SQL Server

To illustrate this tutorial I will take the help of Northwind Database which you can download it from here.

Northwind Database is a sample database from Microsoft. It is very helpful for beginners to learn database programming (through EF, ADO.NET) by creating application that communicates with it.

The download file is a “.zip” file that contains “Northwind.bak” file. So extract the “Northwind.bak” file and “Restore” it to the SQL Server.

Once the “Restore” procedure completes you can use it in your SQL Server.

Steps to Restore “Northwind.bak” file in SQL Server –

  • 1. Open Microsoft SQL Server Management Studio.
  • 2. Right Click the “Databases” and then click “Restore Database”.
  • restore database

  • 3. In the “Restore Database” window, select the “Device” option and click the “…” button on its right side.
  • restore database window

  • 4. The “Select backup devices” window opens up. In this window select “File” as “Backup media type” and click the “Add” button.
  • select backup devices window

  • 5. The “Locate Backup File” window opens . Locate and select the “Northwind.bak” file and click “OK” button.
  • locate backup file window

  • 6. You will find the “Select backup devices” window (step 4) now contains the location of “Northwind.bak”. So click the “OK” button.
  • select backup devices contains northwind backup file

  • 7. Similarly the “Restore Datbase” window of step 3 now contains the location of “Northwind.bak” and is ready to restore it. So click the “OK” button.
  • restore database window ready to restore nothwind backup

  • 8. The restore procedure will proceed and within a few seconds you will receive the message saying “Database ‘Northwind’ restored successfully.”
  • Database Northwind restored successfully

This completes the Northwind Database restore procedure and you are now ready to use this database.

Create EDMX file

An EDMX (Entity Data Model XML) file is an XML file describing the relationship between different models of the Entity Framework. It also provides the graphical representation of these relationships. The EDMX file is the most important part of the Entity Framework through which we do different operations like insert, delete, update and read.

I am now ready to create the EDMX file for the Northwind Database in my Visual Studio. Follow the steps as listed below.

1. Add ADO.NET Entity Data Model

Right Click on the project and go to “Add > New Item”. Select “Add ADO.NET Entity Data Model” and click the “Add” button.

Adding ADO.NET Entity Data Model

2. Entity Data Modal Wizard

Here Visual Studio will ask you to select an option from a number of steps starting with “Choose Model Contents”.

Choose Model Contents

You will get the “Choose Model Contents” window and Visual Studio will ask you “What should the modal contain?” so select the option “EF Designer from database” and click the next button.
Choose Model Contents

Choose Your Data Connection

Here click the “New Connection” button to open the “Connection Properties” window.
choose your data connection

Connection Properties

Enter the name of the server in the text box and select “NORTHWND” in “Connect to a database”. Next click the “OK” button.
connection properties

To test to find out whether the connection properties are correct or not – click the “Test Connection” button.

test connection properties

The “Connection Properties” window will close and you will once again reach the “Choose Your Data Connection” window. This window will now have the settings you set in the “Connection Properties” window.

So check the option that says “Save connection settings in Web.Config as” and click the next button.
choose your data connection after new connection

3. Choose Your Database Objects and Settings

Here Visual Studio will ask you to select “Which database objects do you want to include in your model?”. So select all three – “’Tables’, ‘Views’, ‘Stored Procedures and Functions’”.

Also select the Checkboxes that says:
  • Pluralize or singularize generated object names
  • Include foreign key columns in the model
  • Import selected stored procedures and functions into the entity model

Finally click the “Finish” button.
Choose Your Database Objects and Settings

This completes the creation of EDMX file. The EDMX file for Northwind database will open in the Visual Studio.
northwind database edmx file

Share this article -



This article has been written by the Technical Staff of YogiHosting. Check out other articles on "WordPress, SEO, jQuery, HTML" and more.