Entity Framework In MVC - Part Three

In this article, I will explain how to implement the code-first approach in MVC. When we run the application, the code first creates the new database and tables according to our domain classes. While working on a code-first approach, if a database and tables are already available, then we can use the reverse engineering process.

Now, we will briefly what reverse Engineer is.

Reverse Engineering in the code first approach is used to generate the POCO class, mapping (configuration), and create DbContext classes that are based on an existing database.

See the previous articles for basic details.

Now, we will implement it with MVC. For that, open Visual Studio go to File >> Project >> Web application, and click OK. Then, it will open a window. In that window, select MVC.

Visual Studio

After that, click the OK button.

How does the Entity framework connect with our database?

The database can come from different places like cloud storage, SQL, MySQL, App Data folder, etc. When we connect with the database, then first, we have to create a Dbcontext class.

public MyDbContext() : base("MyConnection")
{
}

When we add the DbContext class, it will first check the connection string name. If it is a connection string name, then simply read the connection string. If it does not get the connection string name, then it will check in the Web. config.

If in any case, it is also not present on the web. config, then when we run the project, it will automatically connect with our LocalDB which is located in the App_Data folder.

LocalDB Connection String

Data Source=(localdb)\mssqllocaldb;

Already, we have seen we can connect the Database in two ways.

  1. Manually, we have to create the class
  2. Reverse Engineer (If already available in our existing database) .

Now, we will follow the Reverse Engineer with a practical approach.

First, we will right-click on the Solution Explorer in our project >> Add >> New item.

Solution Explorer

Then, select the Data in the left panel and select ADO.NET Entity data model and click "Add".

ADO.NET

Code

After that, select Code First from the database and click the "Next" button.

Now, select the "New connection" button and give the server name and our SQL credentials after selecting the database.

New connection

After that, select our existing table like in the below image.

Model

Now, we can see our generated domain class.

 Domain class

Now, we will add a controller. For that, go to Controller >> Add a controller and select MVC 5 Controller – Empty.

MVC

And then, click "Add".

Now, the Index action method is created. So, again, we will write the code for retrieving the data in the Index method.

public ActionResult Index() 
{    
    EmpModel objEntity = new EmpModel();    
    return View(objEntity.Tble_Admin_Details.ToList());    
}

Right-click on the index action method and click the "Add view" option.

Add view

  1. Select the List option in Template.
  2. Select your domain class or model class.
  3. Click Add.

Now, run the project and we will see the result in the below image.

Index

Summary

In this article, we learned some more about the code-first approach in Entity Framework using an existing database and how to implement it. In the next article, we will see the code-first approach using domain class in Entity Framework using MVC.


Similar Articles