Here with Entity Framework we can develop an application using any of the following 3 approaches.

  1. Code First
  2. Model First
  3. Database First
Now we will learn the Code First step-by-step. Open Visual Studio 2012 and go to "File" -> "New" -> "Project...".





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.


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace CodeFirstApproach.Models
  6. {
  7. public class Student
  8. {
  9. public int ID { get; set; }
  10. public string Name { get; set; }
  11. public string Email { get; set; }
  12. public string Class { get; set; }
  13. public string Address { get; set; }
  14. public string Mobile { get; set; }
  15. }
  16. }
Now again right-click on the Model Folder and select Add New Class.




  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data.Entity;
  6. using System.ComponentModel.DataAnnotations.Schema;
  7. namespace CodeFirstApproach.Models
  8. {
  9. public class StudentContext:DbContext
  10. {
  11. public StudentContext(): base("name=DbConnectionString")
  12. {
  13. }
  14. public DbSet<Student> Students { get; set; }
  15. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  16. {
  17. modelBuilder.Entity<Student>().HasKey(b => b.ID);
  18. modelBuilder.Entity<Student>().Property(b => b.ID)
  19. .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  20. base.OnModelCreating(modelBuilder);
  21. }
  22. }
  23. }
Here I am using a connection string. Be sure you define it in your web.config file.


  1. <add name="DbConnectionString"
  2. connectionString="Data Source=INDIA\MSSQLServer2k8;Initial Catalog=MyCodeFirstAPPDB;User ID=sa; Password=india;"
  3. providerName="System.Data.SqlClient" />
Now we need to add a controller then right-click on the Controller Folder and select Add -> Controller.





Here in the Student Controller define an action method for Create/Read/Update/Delete as in the following.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using CodeFirstApproach.Models;
  7. namespace CodeFirstApproach.Controllers
  8. {
  9. public class StudentController : Controller
  10. {
  11. //
  12. // GET: /Student/
  13. StudentContext objContext;
  14. public StudentController()
  15. {
  16. objContext = new StudentContext();
  17. }
  18. public ActionResult Index()
  19. {
  20. var students = objContext.Students.ToList();
  21. return View(students);
  22. }
  23. public ViewResult Details(int id)
  24. {
  25. Student student = objContext.Students.Where(x => x.ID == id).SingleOrDefault();
  26. return View(student);
  27. }
  28. #region Create Student
  29. public ActionResult Create()
  30. {
  31. return View(new Student());
  32. }
  33. [HttpPost]
  34. public ActionResult Create(Student student)
  35. {
  36. objContext.Students.Add(student);
  37. objContext.SaveChanges();
  38. return RedirectToAction("Index");
  39. }
  40. #endregion
  41. #region Edit Student
  42. public ActionResult Edit(int id)
  43. {
  44. Student student = objContext.Students.Where(x => x.ID == id).SingleOrDefault();
  45. return View(student);
  46. }
  47. [HttpPost]
  48. public ActionResult Edit(Student model)
  49. {
  50. Student student = objContext.Students.Where(x => x.ID == model.ID).SingleOrDefault();
  51. if (student != null)
  52. {
  53. objContext.Entry(student).CurrentValues.SetValues(model);
  54. objContext.SaveChanges();
  55. return RedirectToAction("Index");
  56. }
  57. return View(model);
  58. }
  59. #endregion
  60. #region Delete Student
  61. public ActionResult Delete(int id)
  62. {
  63. Student student = objContext.Students.Find(id);
  64. return View(student);
  65. }
  66. [HttpPost]
  67. public ActionResult Delete(int id, Student Model)
  68. {
  69. var student = objContext.Students.Where(x => x.ID == id).SingleOrDefault();
  70. if (student != null)
  71. {
  72. objContext.Students.Remove(student);
  73. objContext.SaveChanges();
  74. }
  75. return RedirectToAction("Index");
  76. }
  77. #endregion
  78. }
  79. }
Now My Views are: Index.cshtml
  1. @model IEnumerable<CodeFirstApproach.Models.Student>
  2. @{
  3. ViewBag.Title = "Index";
  4. }
  5. <h2>Index</h2>
  6. <p>
  7. @Html.ActionLink("Create New", "Create")
  8. </p>
  9. <table>
  10. <tr>
  11. <th>
  12. @Html.DisplayNameFor(model => model.Name)
  13. </th>
  14. <th>
  15. @Html.DisplayNameFor(model => model.Email)
  16. </th>
  17. <th>
  18. @Html.DisplayNameFor(model => model.Class)
  19. </th>
  20. <th>
  21. @Html.DisplayNameFor(model => model.Address)
  22. </th>
  23. <th>
  24. @Html.DisplayNameFor(model => model.Mobile)
  25. </th>
  26. <th></th>
  27. </tr>
  28. @foreach (var item in Model) {
  29. <tr>
  30. <td>
  31. @Html.DisplayFor(modelItem => item.Name)
  32. </td>
  33. <td>
  34. @Html.DisplayFor(modelItem => item.Email)
  35. </td>
  36. <td>
  37. @Html.DisplayFor(modelItem => item.Class)
  38. </td>
  39. <td>
  40. @Html.DisplayFor(modelItem => item.Address)
  41. </td>
  42. <td>
  43. @Html.DisplayFor(modelItem => item.Mobile)
  44. </td>
  45. <td>
  46. @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
  47. @Html.ActionLink("Details", "Details", new { id=item.ID }) |
  48. @Html.ActionLink("Delete", "Delete", new { id=item.ID })
  49. </td>
  50. </tr>
  51. }
  52. </table>
Create.cshtml
  1. @model CodeFirstApproach.Models.Student
  2. @{
  3. ViewBag.Title = "Create";
  4. }
  5. <h2>Create</h2>
  6. @using (Html.BeginForm()) {
  7. @Html.ValidationSummary(true)
  8. <fieldset>
  9. <legend>Student</legend>
  10. <div class="editor-label">
  11. @Html.LabelFor(model => model.Name)
  12. </div>
  13. <div class="editor-field">
  14. @Html.EditorFor(model => model.Name)
  15. @Html.ValidationMessageFor(model => model.Name)
  16. </div>
  17. <div class="editor-label">
  18. @Html.LabelFor(model => model.Email)
  19. </div>
  20. <div class="editor-field">
  21. @Html.EditorFor(model => model.Email)
  22. @Html.ValidationMessageFor(model => model.Email)
  23. </div>
  24. <div class="editor-label">
  25. @Html.LabelFor(model => model.Class)
  26. </div>
  27. <div class="editor-field">
  28. @Html.EditorFor(model => model.Class)
  29. @Html.ValidationMessageFor(model => model.Class)
  30. </div>
  31. <div class="editor-label">
  32. @Html.LabelFor(model => model.Address)
  33. </div>
  34. <div class="editor-field">
  35. @Html.EditorFor(model => model.Address)
  36. @Html.ValidationMessageFor(model => model.Address)
  37. </div>
  38. <div class="editor-label">
  39. @Html.LabelFor(model => model.Mobile)
  40. </div>
  41. <div class="editor-field">
  42. @Html.EditorFor(model => model.Mobile)
  43. @Html.ValidationMessageFor(model => model.Mobile)
  44. </div>
  45. <p>
  46. <input type="submit" value="Create" />
  47. </p>
  48. </fieldset>
  49. }
  50. <div>
  51. @Html.ActionLink("Back to List", "Index")
  52. </div>
  53. @section Scripts {
  54. @Scripts.Render("~/bundles/jqueryval")
  55. }
Details.cshtml
  1. @model CodeFirstApproach.Models.Student
  2. @{
  3. ViewBag.Title = "Delete";
  4. }
  5. <h2>Delete</h2>
  6. <h3>Are you sure you want to delete this?</h3>
  7. <fieldset>
  8. <legend>Student</legend>
  9. <div class="display-label">
  10. @Html.DisplayNameFor(model => model.Name)
  11. </div>
  12. <div class="display-field">
  13. @Html.DisplayFor(model => model.Name)
  14. </div>
  15. <div class="display-label">
  16. @Html.DisplayNameFor(model => model.Email)
  17. </div>
  18. <div class="display-field">
  19. @Html.DisplayFor(model => model.Email)
  20. </div>
  21. <div class="display-label">
  22. @Html.DisplayNameFor(model => model.Class)
  23. </div>
  24. <div class="display-field">
  25. @Html.DisplayFor(model => model.Class)
  26. </div>
  27. <div class="display-label">
  28. @Html.DisplayNameFor(model => model.Address)
  29. </div>
  30. <div class="display-field">
  31. @Html.DisplayFor(model => model.Address)
  32. </div>
  33. <div class="display-label">
  34. @Html.DisplayNameFor(model => model.Mobile)
  35. </div>
  36. <div class="display-field">
  37. @Html.DisplayFor(model => model.Mobile)
  38. </div>
  39. </fieldset>
  40. @using (Html.BeginForm()) {
  41. <p>
  42. <input type="submit" value="Delete" /> |
  43. @Html.ActionLink("Back to List", "Index")
  44. </p>
  45. }
Edit.cshtml
  1. @model CodeFirstApproach.Models.Student
  2. @{
  3. ViewBag.Title = "Edit";
  4. }
  5. <h2>Edit</h2>
  6. @using (Html.BeginForm()) {
  7. @Html.ValidationSummary(true)
  8. <fieldset>
  9. <legend>Student</legend>
  10. @Html.HiddenFor(model => model.ID)
  11. <div class="editor-label">
  12. @Html.LabelFor(model => model.Name)
  13. </div>
  14. <div class="editor-field">
  15. @Html.EditorFor(model => model.Name)
  16. @Html.ValidationMessageFor(model => model.Name)
  17. </div>
  18. <div class="editor-label">
  19. @Html.LabelFor(model => model.Email)
  20. </div>
  21. <div class="editor-field">
  22. @Html.EditorFor(model => model.Email)
  23. @Html.ValidationMessageFor(model => model.Email)
  24. </div>
  25. <div class="editor-label">
  26. @Html.LabelFor(model => model.Class)
  27. </div>
  28. <div class="editor-field">
  29. @Html.EditorFor(model => model.Class)
  30. @Html.ValidationMessageFor(model => model.Class)
  31. </div>
  32. <div class="editor-label">
  33. @Html.LabelFor(model => model.Address)
  34. </div>
  35. <div class="editor-field">
  36. @Html.EditorFor(model => model.Address)
  37. @Html.ValidationMessageFor(model => model.Address)
  38. </div>
  39. <div class="editor-label">
  40. @Html.LabelFor(model => model.Mobile)
  41. </div>
  42. <div class="editor-field">
  43. @Html.EditorFor(model => model.Mobile)
  44. @Html.ValidationMessageFor(model => model.Mobile)
  45. </div>
  46. <p>
  47. <input type="submit" value="Save" />
  48. </p>
  49. </fieldset>
  50. }
  51. <div>
  52. @Html.ActionLink("Back to List", "Index")
  53. </div>
  54. @section Scripts {
  55. @Scripts.Render("~/bundles/jqueryval")
  56. }
Now run the app.



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.