There are 3 different types of development approaches in the Entity Framework.
Code First
Model First
Database First
In this article, only Code First approach is discussed.
Description
Code First Approach is one of the methods to do operations with the database. But, the followed method is different. In Code First Approach, classes are created for domain first and then the output is obtained. That means design is created first. Creation of classes only must be done.
Step-1
Follow the below steps to learn Code First Approach.Here the previous Database i.e Employee table is used.
Create a new MVC Application.
Install Entity Framework if not installed. If not known "How to install Entity Framework?" go to previous chapter Entity Framework Setup.
Go to Solution Explorer->Right Click on the Models Folder->Add->Class As shown in the below figure.
Step-2
Then, the following window will appear. Give the Name as required.
Then, the following Class will be generated.
Step-3
Write the following code in the class. Here, we are creating properties for our fields.
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CodeFirst.Models
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string Salary { get; set; }
public int DepartmentId { get; set; }
}
}
[/csharp]
Step-4
Do the same thing and add one more model class. Give the Name as "StudentContext".
Then, the following code will be generated.
Step-5
Add the following Namespace to your code.
Namespace
using System.Data.Entity;
Step-6
Now, write the following code in the class. Observe the extension we are used i.e DbContext.
[csharp]
namespace CodeFirst.Models
{
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employee { get; set; }
}
}
[/csharp]
Step-7
Now, Build the application.
Add the controller as shown in the below figure.
Then, a new window will appear as shown in the below figure. Select the controller as marked in the figure.
Step-8
Give the Name as you want and click on Add as shown in the below figure.
The following code will be generated automatically in the EmployeeController.cs
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CodeFirst.Controllers
{
public class EmployeeController : Controller
{
//
// GET: /Employee/
public ActionResult Index()
{
return View();
}
//
// GET: /Employee/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Employee/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Employee/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Employee/Edit/5
public ActionResult Edit(int id)
{
return View();
}
//
// POST: /Employee/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Employee/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /Employee/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
[/csharp]
Step-9
Run the Application. then, the following output will appear in the browser.