Data Access In MVC Using Entity Framework

Overview

In this blog, we will see Data Access in MVC, using Entity Framework. Now, the controller responds to the URL requests, gets data from the model and hands it over to the view. The view then renders the data. Model can be entities or business objects. Please refer to my previous article,  Models in MVC Applications. In that article, we have Students entity as-


This entity is actually residing in the Models folder of our MVC Application. We have Students class with the auto increment properties. Within our Controller StudentsController, we have created an instance of student object and we actually hard coded our values within our controller action method and passed the student object for rendering as-


We want to retrieve the data from our database table. Thus, let’s see how to achieve it.

We have a table called students as-



Here, we want to retrieve the data from this table. We will pass this data to our student object and hand that object to the view, which will then render various details. First, install Entity Framework in the solution with the help of Manage NuGet Package Manager. Let’s add StudentsContext.cs to our model folder.


As the DbContext class is present in System.Data.Entitynamespace, StudentContext class is getting inherited from DbContext class. The purpose of this class is to establish a connection to our database.

The next step is to add a connection string in our web.config file.
  1. <connectionStrings>  
  2. <addname="StudentContext"connectionString="server=DESKTOP-QOMPPF7;Database=TEST ; integrated security=SSPI ;"providerName="System.Data.SqlClient"/>  
  3. </connectionStrings>  
The name of our connection string is StudentContext, which should match with the name of the class file, which is StudentContext.cs.

The next step is to change our code in our StudentsController. At the moment, we had hard coded our data. Remove the hard coded code. Now, we need to establish a connection, so, how to establish a connection? We had created StudentContext class, in which we will specify the instance in our controller, which is going to use the connection string, which we specified in our web.configfile.



Now, the next step is add the given line in your global.asax file in Application_onstart event handler as- 


Thus, what is SetInitializer function? The purpose of setinitializer function is when you look at the intellisense. Basically, it is used to initialize the database, if you don’t have a database created. It will create a new database for you, create tables for you and populate some sample data as well.

We already have a database table, so we don’t want any initialization strategy. Thus, we are passing null. I don’t want any initialization as such.

Now, save the solution and run the app, as given below.


Notice, in the URL, we are passing an Id as 1 because we had specified in our action parameter as Id. Thus, it is retrieving the data, whose Id is 1, using a Single link method.

Conclusion

This was all about data access in MVC, using Entity Framework. Hope this blog was helpful.