Entity Framework In MVC - Part Three

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

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 and go to File >> Project >> Web application and click OK. Then, it will open a window. In that window, select MVC.

Entity Framework

After that, click the OK button.

How does 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 Dbcontext class.

  1. publicMyDbContext() : base("MyConection")    
  2. {}    

When we add the DbContext class, it will first check the connection string name. If it will find 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.

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

 LocalDB Connection String

  1. 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.

Entity Framework

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

Entity Framework

Entity Framework

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

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

Entity Framework

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

Entity Framework

Now, we can see our generated domain class.

Entity Framework

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

Entity Framework

And then, click "Add".

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

  1. publicActionResult Index() {    
  2.    EmpModel objEntity = new EmpModel();    
  3.    return View(objEntity.Tble_Admin_Details.ToList());    
  4. }    

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

Entity Framework
  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 like in below image.

Entity Framework

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.