Microsoft has created Entity Framework (short form EF) for helping developers to automate database related operations in C#, VB, C++ and other dot net based programming languages. Due to EF, developers are not needed to do the lengthy ADO.NET programming as EF takes all it’s care.
Here in this tutorial I will help you to get stated with Entity Framework.
You can install Entity Framework through Nuget in Visual Studio. This procedure is same for all versions of Visual Studio.
First, right click on your Project in Solution Explorer and select Manage Nuget Packages.
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.
Next, click I Agree button on the License Acceptance window. This will start the installation of EF.
It takes less than a minute to complete, and you will soon see Successfully installed message in the Output window.
To illustrate this tutorial I will take the help of Northwind Database which you can download it from here.
The download file is in a .zip format. Extract the Northwind.bak from it.
Next, Restore it to your SQL Server.
Steps to Restore Northwind.bak file in SQL Server –
This completes the Northwind Database restore procedure and you are now ready to use this database.
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.
Right Click on the project and go to Add > New Item. Select Add ADO.NET Entity Data Model and click the “Add” button.
Here Visual Studio will ask you to select an option from a number of steps starting with 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 saying EF Designer from database and click the next button.
Here click the New Connection button to open the Connection Properties window.
Enter the name of the server in the text box and select NORTHWND in Connect to a database. Next click the “OK” button.
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.
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’.
Finally at last, click the Finish button.
This completes the creation of EDMX file. The EDMX file for Northwind database will open up in the Visual Studio.