In this article I will explain a simple tutorial with an example, how to use Entity Framework Code First approach with existing Database in ASP.Net MVC.
This article will explain how to configure Entity Framework Code first from database in ASP.Net MVC.
Database
1. First you need to download the SQL file using the following link.
2. Once downloaded, you need to execute the SQL file in SQL Server Management Studio.
3. After execution you will see the following Database.
The Database also contains the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
The Table consists of following records.
Creating new ASP.Net MVC Project
1. Open Visual Studio and from Start section click on New Project.
2. From the New Project Dialog window, select ASP.Net Web Application option. Then you need to set a suitable Name for your project and also you can set its Location where you want to create the Project.
3. From the New ASP.Net Project Dialog, select Empty Template and make sure the MVC CheckBox is checked as shown below.
For this sample, you can uncheck the Microsoft Azure’s “Host in the cloud” option.
Then, Click on OK. Your MVC Project is now ready.
Configuring and connecting Entity Framework Code First from database
Now I will explain the steps to configure and connect Entity Framework Code first from database.
You will need to add Entity Data Model to your project by right clicking the Solution Explorer and then click on Add and then New Item option of the Context Menu.
From the Add New Item window, select ADO.NET Entity Data Model and set its Name as CodeFirstDBContext and then click Add.
Then the Entity Data Model Wizard will open up where you need to select Code first from database option.
Now the wizard will ask you to connect and configure the Connection String to the database.
You will need to select the
1. SQL Server Instance
2. Database
And then click Test Connection to make sure all settings are correct.
Once the Connection String is generated, click Next button to move to the next step.
Now you will need to choose the Tables you need to connect and work with Entity Framework. Here Customers Table is selected.
Then, Click on Finish.
The above was the last step and you should now have the Entity Data Model ready with the Customers Table of the CodeFirst Database.
Finally, the CodeFirstDBContext will look as shown below.
namespace EF_CodeFirst_ExistingDB_MVC
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
public partial class CodeFirstDBContext : DbContext
{
public CodeFirstDBContext()
: base("name=CodeFirst")
{
}
public virtual DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>()
.Property(e => e.Name)
.IsUnicode(false);
modelBuilder.Entity<Customer>()
.Property(e => e.Country)
.IsUnicode(false);
}
}
}
Controller
The Entity Framework is now configured and hence now we can create a Controller and write code to fetch the records from the Customers Table of the CodeFirst Database.
The Controller consists of the following Action method.
Action method for handling GET operation
Inside this Action method, the Customer records are fetched from database using Entity Framework and returned to the View.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
using (CodeFirstDBContext context = new CodeFirstDBContext())
{
return View(context.Customers.ToList());
}
}
}
View
Now you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.
The Name of the View is set to Index, the Template option is set to Empty, the Model class is set to Customer Entity (the one we have generated using Entity Framework Code first from database) and finally, the Data context class is set to CodeFirstDBContext.
Inside the View, in the very first line the Customer Model is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
@model IEnumerable<EF_CodeFirst_ExistingDB_MVC.Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table cellpadding="0" cellspacing="0">
<tr>
<th>CustomerId</th>
<th>Name</th>
<th>Country</th>
</tr>
@foreach (Customer customer in Model)
{
<tr>
<td>@customer.CustomerId</td>
<td>@customer.Name</td>
<td>@customer.Country</td>
</tr>
}
</table>
</body>
</html>
Screenshot
Downloads