Here with Entity Framework we can develop an application using any of the following 3 approaches.
- Code First
- Model First
- Database First


Now right-click on the project in the Solution Explorer and select Manage NuGet Packages.




Now right-click on the Model and select Add New Class.


In your Class define a property or column name that you want in your SQL Server Table.

- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace CodeFirstApproach.Models
- {
- public class Student
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public string Email { get; set; }
- public string Class { get; set; }
- public string Address { get; set; }
- public string Mobile { get; set; }
- }
- }


- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Data.Entity;
- using System.ComponentModel.DataAnnotations.Schema;
- namespace CodeFirstApproach.Models
- {
- public class StudentContext:DbContext
- {
- public StudentContext(): base("name=DbConnectionString")
- {
- }
- public DbSet<Student> Students { get; set; }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- modelBuilder.Entity<Student>().HasKey(b => b.ID);
- modelBuilder.Entity<Student>().Property(b => b.ID)
- .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
- base.OnModelCreating(modelBuilder);
- }
- }
- }

- <add name="DbConnectionString"
- connectionString="Data Source=INDIA\MSSQLServer2k8;Initial Catalog=MyCodeFirstAPPDB;User ID=sa; Password=india;"
- providerName="System.Data.SqlClient" />


Here in the Student Controller define an action method for Create/Read/Update/Delete as in the following.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using CodeFirstApproach.Models;
- namespace CodeFirstApproach.Controllers
- {
- public class StudentController : Controller
- {
- //
- // GET: /Student/
- StudentContext objContext;
- public StudentController()
- {
- objContext = new StudentContext();
- }
- public ActionResult Index()
- {
- var students = objContext.Students.ToList();
- return View(students);
- }
- public ViewResult Details(int id)
- {
- Student student = objContext.Students.Where(x => x.ID == id).SingleOrDefault();
- return View(student);
- }
- #region Create Student
- public ActionResult Create()
- {
- return View(new Student());
- }
- [HttpPost]
- public ActionResult Create(Student student)
- {
- objContext.Students.Add(student);
- objContext.SaveChanges();
- return RedirectToAction("Index");
- }
- #endregion
- #region Edit Student
- public ActionResult Edit(int id)
- {
- Student student = objContext.Students.Where(x => x.ID == id).SingleOrDefault();
- return View(student);
- }
- [HttpPost]
- public ActionResult Edit(Student model)
- {
- Student student = objContext.Students.Where(x => x.ID == model.ID).SingleOrDefault();
- if (student != null)
- {
- objContext.Entry(student).CurrentValues.SetValues(model);
- objContext.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(model);
- }
- #endregion
- #region Delete Student
- public ActionResult Delete(int id)
- {
- Student student = objContext.Students.Find(id);
- return View(student);
- }
- [HttpPost]
- public ActionResult Delete(int id, Student Model)
- {
- var student = objContext.Students.Where(x => x.ID == id).SingleOrDefault();
- if (student != null)
- {
- objContext.Students.Remove(student);
- objContext.SaveChanges();
- }
- return RedirectToAction("Index");
- }
- #endregion
- }
- }
- @model IEnumerable<CodeFirstApproach.Models.Student>
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- <p>
- @Html.ActionLink("Create New", "Create")
- </p>
- <table>
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.Name)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Email)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Class)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Address)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Mobile)
- </th>
- <th></th>
- </tr>
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.Name)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Email)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Class)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Address)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Mobile)
- </td>
- <td>
- @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
- @Html.ActionLink("Details", "Details", new { id=item.ID }) |
- @Html.ActionLink("Delete", "Delete", new { id=item.ID })
- </td>
- </tr>
- }
- </table>
- @model CodeFirstApproach.Models.Student
- @{
- ViewBag.Title = "Create";
- }
- <h2>Create</h2>
- @using (Html.BeginForm()) {
- @Html.ValidationSummary(true)
- <fieldset>
- <legend>Student</legend>
- <div class="editor-label">
- @Html.LabelFor(model => model.Name)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Name)
- @Html.ValidationMessageFor(model => model.Name)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Email)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Email)
- @Html.ValidationMessageFor(model => model.Email)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Class)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Class)
- @Html.ValidationMessageFor(model => model.Class)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Address)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Address)
- @Html.ValidationMessageFor(model => model.Address)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Mobile)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Mobile)
- @Html.ValidationMessageFor(model => model.Mobile)
- </div>
- <p>
- <input type="submit" value="Create" />
- </p>
- </fieldset>
- }
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }
- @model CodeFirstApproach.Models.Student
- @{
- ViewBag.Title = "Delete";
- }
- <h2>Delete</h2>
- <h3>Are you sure you want to delete this?</h3>
- <fieldset>
- <legend>Student</legend>
- <div class="display-label">
- @Html.DisplayNameFor(model => model.Name)
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.Name)
- </div>
- <div class="display-label">
- @Html.DisplayNameFor(model => model.Email)
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.Email)
- </div>
- <div class="display-label">
- @Html.DisplayNameFor(model => model.Class)
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.Class)
- </div>
- <div class="display-label">
- @Html.DisplayNameFor(model => model.Address)
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.Address)
- </div>
- <div class="display-label">
- @Html.DisplayNameFor(model => model.Mobile)
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.Mobile)
- </div>
- </fieldset>
- @using (Html.BeginForm()) {
- <p>
- <input type="submit" value="Delete" /> |
- @Html.ActionLink("Back to List", "Index")
- </p>
- }
- @model CodeFirstApproach.Models.Student
- @{
- ViewBag.Title = "Edit";
- }
- <h2>Edit</h2>
- @using (Html.BeginForm()) {
- @Html.ValidationSummary(true)
- <fieldset>
- <legend>Student</legend>
- @Html.HiddenFor(model => model.ID)
- <div class="editor-label">
- @Html.LabelFor(model => model.Name)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Name)
- @Html.ValidationMessageFor(model => model.Name)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Email)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Email)
- @Html.ValidationMessageFor(model => model.Email)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Class)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Class)
- @Html.ValidationMessageFor(model => model.Class)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Address)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Address)
- @Html.ValidationMessageFor(model => model.Address)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Mobile)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Mobile)
- @Html.ValidationMessageFor(model => model.Mobile)
- </div>
- <p>
- <input type="submit" value="Save" />
- </p>
- </fieldset>
- }
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }

There is no database created so click on Create New.

Now see your DB as in the following:

Now add more records as in the following:

Now click on Edit.

Now click on Details.

Now click on Delete.

Rahul Kumar SaxenaPosted Apr 27, 2015, 1:38 AM
Thanks a lot shraddha G
Shraddha GPosted Apr 27, 2015, 12:25 AM
Rahul Saxena .. I saw this article in Article Of The Day http://www.asp.net/community/articles Congratulations....
Rahul Kumar SaxenaPosted Apr 16, 2015, 10:46 PM
Thanku Shraddha G...
Shraddha GPosted Apr 16, 2015, 1:35 PM
Thank u so much for this... I was looking same... Very Helpful
Rahul Kumar SaxenaPosted Apr 16, 2015, 11:05 AM
Thanks Nitin bro...
Rahul Kumar SaxenaPosted Apr 16, 2015, 8:02 AM
Thanks a lot Mahesh Sir...
Mahesh ChandPosted Apr 16, 2015, 5:46 AM
Nice Rahul.
NitinPosted Apr 16, 2015, 4:04 AM
Good one
Arindam GhoshPosted Apr 16, 2015, 3:25 AM
I have two questions here,1. Can we use Code First for the existing Database? 2. In my DB, I have stored Procedure, How can I use that? Can you please tell me elaborately
Gowtham RajamanickamPosted Apr 16, 2015, 2:44 AM
nice
Gowtham RajamanickamPosted Apr 16, 2015, 2:44 AM
Good one
Rahul Kumar SaxenaPosted Apr 16, 2015, 2:25 AM
Thanks Syed Shanu...
Rahul Kumar SaxenaPosted Apr 16, 2015, 2:24 AM
Thanks Jaiprakash Barnwal...
Rahul Kumar SaxenaPosted Apr 16, 2015, 2:24 AM
Thanks Manish Kumar Choudhary...
Rahul Kumar SaxenaPosted Apr 16, 2015, 2:24 AM
Thanks Santhakumar Munuswamy...
Syed ShanuPosted Apr 16, 2015, 2:15 AM
Nice :)
Jaiprakash BarnwalPosted Apr 16, 2015, 1:42 AM
Nice article. Would be interested in knowing how to handle model change in the same, please guide.
Manish Kumar ChoudharyPosted Apr 16, 2015, 1:21 AM
Nice one.
Santhakumar MunuswamyPosted Apr 15, 2015, 11:20 PM
Thanks for nice article
Santhakumar MunuswamyPosted Apr 15, 2015, 11:20 PM
Good work