In the end of this article, you will be able to develop a project in MVC 5 using Entity Framework (Database First Approach).
Starting with the tutorial, create a new ASP.NET web application project as shown below.
The default application will be set up as shown below.
MVC project has been set up. Now, make a new C# class library project which will act as a DAL layer and the Entity Framework will be handled through this project.
Both projects are set up. Now, make a database and create a table from which the data will be retrieved and shown on the View page.
Open SQL Server, and save your server name for future use.
In SQL server, make a new database. Create a table in it that is called ‘Item’ and add some dummy data to it. This is how your table will look.
After adding the C# class library project, install Entity framework NuGet Package for both the projects.
Right-click on Project Solution and click ‘Manage NuGet Package for solution’.
Now, click on ‘Browse’ and enter ‘Entity Framework’ in the search field. Now, click on the first search result and it will then give you options on the right side of the screen. Select both the projects, i.e. C# class library and MVC and then click "Install".
After that, the License Acceptance window will open up; click ‘I Accept’.
Finally, Entity Framework will be installed successfully in both projects. You can recheck by clicking on the ‘References’ option which is shown under your project.
Now, we have to use Database First approach and connect the database with the C# class library project that we made and export the tables to the project. For that, we have to follow the following steps.
Right-click on the class library project, select Add->New Item. Then, from the options on the left side, choose ‘Data’ and then select ‘ADO.NET Entity Data Model’.
After selecting ADO.NET Entity data model, it will take you to the next screen on which you will select EF designer from the database.
Then, click the Next button. The next screen will ask you to make a connection with the database. Click on ‘New Connection’ and select Microsoft SQL Server and check the box below which is ‘Save connection string in App.config’. Then, click Next. All this process is shown below.
The data source will be filled up automatically which you have given on the previous screen and you have to fill up the Server name field which in my case is ‘.\SQLEXPRESS’. Then, select one option from below. Either ‘select or enter database name’ or ‘attach a database file’. I have chosen the first one and then from the drop-down, select your database name. Then, click OK.
Here, your connection string is automatically generated and is shown. Copy it and save it somewhere for future use. Click "Next".
Click "Next".
Whatever is in your database can be exported to the project. Select the option which you want to export. For this tutorial, I am just exporting the tables so I have selected the first option only. Click "Finish".
After clicking Finish, you will see that this window will be previewed to you. The diagram is automatically generated from your database relationships. Other than that, classes will be generated automatically as Item class is generated in this case.
This is the Context class which will be automatically generated.
This is the Item class generated from the table Item in the database.
Now, add the reference of this class library project to your UI/MVC project. Right on the ‘Reference’ option shown under the project of MVC/UI project, select "Add Reference". Now, select Projects option from the left and check the checkbox of your class library project and click OK.
This is how your reference will be added. DAL can be seen in above image for my case.
Now, right click on DAL project and create a new folder of ‘Repository’. Then, right-click on Repository folder and create a new class ‘ItemRepository.cs’. And then, write a function to get all items from the database. This is how it will look.
Now, on web application project, expand Controllers folder and open HomeController. Edit the ‘Index’ action method which will now call the function of ItemRepository to get all items, as shown below.
After calling the repository function, I have saved it in a ViewBag so that on the View page, I can access the data from ViewBag.
Now expand the ‘Views’ folder and then expand the ‘Home’ folder. Open the ‘Index’ page and make these changes to it.
Now, add a connection string in the web application project. Open ‘Web.config’ file and add the connection string which was saved in the above steps while we were making a connection with the database. Here is my connection string.
- ‘<add name="MVC_EF_DbFirstEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=MVC_EF_DbFirst;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />'
Now, run the application and you can see the result. All the items are shown in the grid form which were fetched from the database.
This is how MVC works with Entity Framework Database First Approach.
I hope you will like this article and it will help you.
The source code and DB backup file are attached. You can download and use these.