In my previous article I explained what Code First Approach in MVC with Entity Framework is. Now in this article I will show how to create a Primary Key and Foreign Key using the code first approach in MVC with Entity Framework.

Here I will create 2 tables, Students and Course. Both tables will have a Primary Key and the Student table will reference the course table with CourseID as the Foreign Key.

Now open Visual Studio 2012 and select New Project.

mvc 4 web application

internet application

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

manage nuget package

Now here in this project I will create 2 tables, one is the Student table and the second one is the Course table. Both tables will have a Primary Key and the Student and Course tables will have a Foreign Key, CourseID. So here I will create 2 clasess in the Model Folder.

Student.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. using System.Linq;
  6. using System.Web;
  7. namespace CodeFirstApproachWithPrimaryForeignKey.Models
  8. {
  9. public class Student
  10. {
  11. public Student()
  12. {
  13. }
  14. [Key]
  15. public int Id { get; set; }
  16. public string Name { get; set; }
  17. [DataType(DataType.Date),
  18. DisplayFormat(DataFormatString = "{0:dd/MM/yy}",
  19. ApplyFormatInEditMode = true)]
  20. public DateTime? DateOfBirth { get; set; }
  21. public string EmailId { get; set; }
  22. public string Address { get; set; }
  23. public string City { get; set; }
  24. public int CourseId { get; set; }
  25. public Course Course { get; set; } // Navigation Property
  26. [NotMapped]
  27. public string CourseName { get; set; }
  28. }
  29. }
Here CourseName will not be a field because I set it as NotMapped.
Now Course.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Web;
  6. namespace CodeFirstApproachWithPrimaryForeignKey.Models
  7. {
  8. public class Course
  9. {
  10. public Course()
  11. {
  12. }
  13. [Key]
  14. public int CourseId { get; set; }
  15. public string CourseName { get; set; }
  16. public List<Student> Students { get; set; } // Navigation property
  17. }
  18. }
Now again right-click on the Model folder and add the new class StudentDBContext.cs.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations.Schema;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Web;
  7. namespace CodeFirstApproachWithPrimaryForeignKey.Models
  8. {
  9. public class StudentDBContext : DbContext
  10. {
  11. public StudentDBContext()
  12. : base("StudentDbContext")
  13. {
  14. }
  15. public DbSet<Student> Students { get; set; }
  16. public DbSet<Course> Courses { get; set; }
  17. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  18. {
  19. modelBuilder.Entity<Course>().HasKey(p => p.CourseId);
  20. modelBuilder.Entity<Course>().Property(c => c.CourseId)
  21. .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  22. modelBuilder.Entity<Student>().HasKey(b => b.Id);
  23. modelBuilder.Entity<Student>().Property(b => b.Id)
  24. .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  25. modelBuilder.Entity<Student>().HasRequired(p => p.Course)
  26. .WithMany(b => b.Students).HasForeignKey(b => b.CourseId);
  27. base.OnModelCreating(modelBuilder);
  28. }
  29. }
  30. }

Here In this StudentDBContext.cs you can see I am using OnModelCreating. Here I define what will be the Primary Key and what will be the Foreign Key.

Now right-click on Controller then select Add -> Controller -> Student.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using CodeFirstApproachWithPrimaryForeignKey.Models;
  7. namespace CodeFirstApproachWithPrimaryForeignKey.Controllers
  8. {
  9. public class StudentController : Controller
  10. {
  11. //
  12. // GET: /Student/
  13. StudentDBContext objContext;
  14. public StudentController()
  15. {
  16. objContext = new StudentDBContext();
  17. }
  18. #region List and Details student
  19. public ActionResult Index()
  20. {
  21. var students = (from p in objContext.Students
  22. join f in objContext.Courses
  23. on p.CourseId equals f.CourseId
  24. select new
  25. {
  26. Id = p.Id,
  27. Name = p.Name,
  28. DateOfBirth = p.DateOfBirth,
  29. EmailId = p.EmailId,
  30. Address = p.Address,
  31. City = p.City,
  32. CourseName = f.CourseName
  33. }).ToList()
  34. .Select(x => new Student()
  35. {
  36. Id = x.Id,
  37. Name = x.Name,
  38. DateOfBirth = x.DateOfBirth,
  39. EmailId = x.EmailId,
  40. Address = x.Address,
  41. City = x.City,
  42. CourseName = x.CourseName
  43. });
  44. return View(students.ToList());
  45. }
  46. public ViewResult Details(int id)
  47. {
  48. //Student student = objContext.Students.Where(x => x.Id == id).SingleOrDefault();
  49. var student = (from p in objContext.Students
  50. join f in objContext.Courses
  51. on p.CourseId equals f.CourseId
  52. where (p.Id==id)
  53. select new
  54. {
  55. Id = p.Id,
  56. Name = p.Name,
  57. DateOfBirth = p.DateOfBirth,
  58. EmailId = p.EmailId,
  59. Address = p.Address,
  60. City = p.City,
  61. CourseName = f.CourseName
  62. }).ToList()
  63. .Select(x => new Student()
  64. {
  65. Id = x.Id,
  66. Name = x.Name,
  67. DateOfBirth = x.DateOfBirth,
  68. EmailId = x.EmailId,
  69. Address = x.Address,
  70. City = x.City,
  71. CourseName = x.CourseName
  72. }).SingleOrDefault();
  73. return View(student);
  74. }
  75. #endregion
  76. #region Create student
  77. public ActionResult Create()
  78. {
  79. var data = from p in objContext.Courses
  80. select new
  81. {
  82. CourseID = p.CourseId,
  83. CourseName = p.CourseName
  84. };
  85. SelectList list = new SelectList(data, "CourseID", "CourseName");
  86. ViewBag.Roles = list;
  87. return View(new Student());
  88. }
  89. [HttpPost]
  90. public ActionResult Create(Student student)
  91. {
  92. objContext.Students.Add(student);
  93. objContext.SaveChanges();
  94. return RedirectToAction("Index");
  95. }
  96. #endregion
  97. #region Edit student
  98. public ActionResult Edit(int id)
  99. {
  100. var data = from p in objContext.Courses
  101. select new
  102. {
  103. CourseID = p.CourseId,
  104. CourseName = p.CourseName
  105. };
  106. SelectList list = new SelectList(data, "CourseID", "CourseName");
  107. ViewBag.Roles = list;
  108. Student student = objContext.Students.Where(x => x.Id == id).SingleOrDefault();
  109. return View(student);
  110. }
  111. [HttpPost]
  112. public ActionResult Edit(Student model)
  113. {
  114. Student student = objContext.Students.Where(x => x.Id == model.Id).SingleOrDefault();
  115. if (student != null)
  116. {
  117. objContext.Entry(student).CurrentValues.SetValues(model);
  118. objContext.SaveChanges();
  119. return RedirectToAction("Index");
  120. }
  121. return View(model);
  122. }
  123. #endregion
  124. #region Delete student
  125. public ActionResult Delete(int id)
  126. {
  127. Student student = objContext.Students.Find(id);
  128. return View(student);
  129. }
  130. [HttpPost]
  131. public ActionResult Delete(int id, Student model)
  132. {
  133. var student = objContext.Students.Where(x => x.Id == id).SingleOrDefault();
  134. if (student != null)
  135. {
  136. objContext.Students.Remove(student);
  137. objContext.SaveChanges();
  138. }
  139. return RedirectToAction("Index");
  140. }
  141. #endregion
  142. }
  143. }
Views Are for Create/Read/Details/Edit/Delete
Index.cshtml
  1. <h2>Showing All Students</h2>
  2. <p>
  3. @Html.ActionLink("Create New", "Create")
  4. </p>
  5. <table style="width:100%;">
  6. <tr>
  7. <th >
  8. @Html.DisplayNameFor(model => model.Name)
  9. </th>
  10. <th style="width:20%;">
  11. @Html.DisplayNameFor(model => model.DateOfBirth)
  12. </th>
  13. <th style="width:20%;">
  14. @Html.DisplayNameFor(model => model.EmailId)
  15. </th>
  16. <th>
  17. @Html.DisplayNameFor(model => model.Address)
  18. </th>
  19. <th>
  20. @Html.DisplayNameFor(model => model.City)
  21. </th>
  22. <th>
  23. @Html.DisplayNameFor(model => model.CourseName)
  24. </th>
  25. <th></th>
  26. </tr>
  27. @foreach (var item in Model) {
  28. <tr>
  29. <td>
  30. @Html.DisplayFor(modelItem => item.Name)
  31. </td>
  32. <td>
  33. @Html.DisplayFor(modelItem => item.DateOfBirth)
  34. </td>
  35. <td>
  36. @Html.DisplayFor(modelItem => item.EmailId)
  37. </td>
  38. <td>
  39. @Html.DisplayFor(modelItem => item.Address)
  40. </td>
  41. <td>
  42. @Html.DisplayFor(modelItem => item.City)
  43. </td>
  44. <td>
  45. @Html.DisplayFor(modelItem => item.CourseName)
  46. </td>
  47. <td>
  48. @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
  49. @Html.ActionLink("Details", "Details", new { id=item.Id }) |
  50. @Html.ActionLink("Delete", "Delete", new { id=item.Id })
  51. </td>
  52. </tr>
  53. }
  54. </table>
Create.cshtml
  1. @model CodeFirstApproachWithPrimaryForeignKey.Models.Student
  2. @{
  3. ViewBag.Title = "Create";
  4. }
  5. <h2>Create</h2>
  6. @using (Html.BeginForm())
  7. {
  8. @Html.ValidationSummary(true)
  9. <fieldset>
  10. <legend>Student</legend>
  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.DateOfBirth)
  20. </div>
  21. <div class="editor-field">
  22. @Html.EditorFor(model => model.DateOfBirth)
  23. @Html.ValidationMessageFor(model => model.DateOfBirth)
  24. </div>
  25. <div class="editor-label">
  26. @Html.LabelFor(model => model.EmailId)
  27. </div>
  28. <div class="editor-field">
  29. @Html.EditorFor(model => model.EmailId)
  30. @Html.ValidationMessageFor(model => model.EmailId)
  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.City)
  41. </div>
  42. <div class="editor-field">
  43. @Html.EditorFor(model => model.City)
  44. @Html.ValidationMessageFor(model => model.City)
  45. </div>
  46. <div class="editor-label">
  47. @Html.LabelFor(model => model.CourseId)
  48. </div>
  49. <div class="editor-field">
  50. @Html.DropDownListFor(m=>m.CourseId, ViewBag.Roles as SelectList, "Select ...", new { @class = "myClass", style = "width: 250px;" })
  51. @* @Html.EditorFor(model => model.CourseId)*@
  52. @Html.ValidationMessageFor(model => model.CourseId)
  53. </div>
  54. <p>
  55. <input type="submit" value="Create" />
  56. </p>
  57. </fieldset>
  58. }
  59. <div>
  60. @Html.ActionLink("Back to List", "Index")
  61. </div>
  62. @section Scripts {
  63. @Scripts.Render("~/bundles/jqueryval")
  64. }
Delete.cshtml
  1. @model CodeFirstApproachWithPrimaryForeignKey.Models.Student
  2. @{
  3. ViewBag.Title = "Delete";
  4. }
  5. <h2>Delete</h2>
  6. <h3>Are you sure you want to delete this?</h3>
  7. <table>
  8. <tr>
  9. <td>@Html.DisplayNameFor(model => model.Name)</td>
  10. <td>@Html.DisplayFor(model => model.Name)</td>
  11. </tr>
  12. <tr>
  13. <td>@Html.DisplayNameFor(model => model.DateOfBirth)</td>
  14. <td>@Html.DisplayFor(model => model.DateOfBirth)</td>
  15. </tr>
  16. <tr>
  17. <td>@Html.DisplayNameFor(model => model.EmailId)</td>
  18. <td>@Html.DisplayFor(model => model.EmailId)</td>
  19. </tr>
  20. <tr>
  21. <td>@Html.DisplayNameFor(model => model.Address)</td>
  22. <td>@Html.DisplayFor(model => model.Address)</td>
  23. </tr>
  24. <tr>
  25. <td>@Html.DisplayNameFor(model => model.City)</td>
  26. <td>@Html.DisplayFor(model => model.City)</td>
  27. </tr>
  28. <tr>
  29. <td>@Html.DisplayNameFor(model => model.CourseName)</td>
  30. <td>@Html.DisplayFor(model => model.CourseName)</td>
  31. </tr>
  32. <tr style="background-color: orange; padding: 25px;">
  33. <td></td>
  34. <td>@Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
  35. @Html.ActionLink("Back to List", "Index")</td>
  36. </tr>
  37. </table>
  38. @using (Html.BeginForm())
  39. {
  40. <table>
  41. <tr style="background-color: orange; padding: 25px;">
  42. <td></td>
  43. <td>
  44. <input type="submit" value="Delete" />
  45. @Html.ActionLink("Back to List", "Index")
  46. </td>
  47. </tr>
  48. </table>
  49. }
Details.cshtml
  1. @model CodeFirstApproachWithPrimaryForeignKey.Models.Student
  2. @{
  3. ViewBag.Title = "Details";
  4. }
  5. <h2>Details Of Student</h2>
  6. <table>
  7. <tr>
  8. <td>@Html.DisplayNameFor(model => model.Name)</td>
  9. <td>@Html.DisplayFor(model => model.Name)</td>
  10. </tr>
  11. <tr>
  12. <td>@Html.DisplayNameFor(model => model.DateOfBirth)</td>
  13. <td>@Html.DisplayFor(model => model.DateOfBirth)</td>
  14. </tr>
  15. <tr>
  16. <td>@Html.DisplayNameFor(model => model.EmailId)</td>
  17. <td>@Html.DisplayFor(model => model.EmailId)</td>
  18. </tr>
  19. <tr>
  20. <td>@Html.DisplayNameFor(model => model.Address)</td>
  21. <td>@Html.DisplayFor(model => model.Address)</td>
  22. </tr>
  23. <tr>
  24. <td>@Html.DisplayNameFor(model => model.City)</td>
  25. <td>@Html.DisplayFor(model => model.City)</td>
  26. </tr>
  27. <tr>
  28. <td>@Html.DisplayNameFor(model => model.CourseName)</td>
  29. <td>@Html.DisplayFor(model => model.CourseName)</td>
  30. </tr>
  31. <tr style="background-color: orange; padding: 25px;">
  32. <td></td>
  33. <td>@Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
  34. @Html.ActionLink("Back to List", "Index")</td>
  35. </tr>
  36. </table>
Edit.cshtml
  1. @model CodeFirstApproachWithPrimaryForeignKey.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.DateOfBirth)
  20. </div>
  21. <div class="editor-field">
  22. @Html.EditorFor(model => model.DateOfBirth)
  23. @Html.ValidationMessageFor(model => model.DateOfBirth)
  24. </div>
  25. <div class="editor-label">
  26. @Html.LabelFor(model => model.EmailId)
  27. </div>
  28. <div class="editor-field">
  29. @Html.EditorFor(model => model.EmailId)
  30. @Html.ValidationMessageFor(model => model.EmailId)
  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.City)
  41. </div>
  42. <div class="editor-field">
  43. @Html.EditorFor(model => model.City)
  44. @Html.ValidationMessageFor(model => model.City)
  45. </div>
  46. <div class="editor-label">
  47. @Html.LabelFor(model => model.CourseId)
  48. </div>
  49. <div class="editor-field">
  50. @Html.DropDownListFor(m=>m.CourseId, ViewBag.Roles as SelectList, "Select ...", new { @class = "myClass", style = "width: 250px;" })
  51. @* @Html.EditorFor(model => model.CourseId)*@
  52. @Html.ValidationMessageFor(model => model.CourseId)
  53. </div>
  54. <p>
  55. <input type="submit" value="Save" />
  56. </p>
  57. </fieldset>
  58. }
  59. <div>
  60. @Html.ActionLink("Back to List", "Index")
  61. </div>
  62. @section Scripts {
  63. @Scripts.Render("~/bundles/jqueryval")
  64. }
Now again right-click on Controller then select Add-> Controller -> Course.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using CodeFirstApproachWithPrimaryForeignKey.Models;
  7. namespace CodeFirstApproachWithPrimaryForeignKey.Controllers
  8. {
  9. public class CourseController : Controller
  10. {
  11. //
  12. // GET: /Course/
  13. StudentDBContext objContext;
  14. public CourseController()
  15. {
  16. objContext = new StudentDBContext();
  17. }
  18. #region List and Details course
  19. public ActionResult Index()
  20. {
  21. var courses = objContext.Courses.ToList();
  22. return View(courses);
  23. }
  24. public ViewResult Details(int id)
  25. {
  26. Course course = objContext.Courses.Where(x => x.CourseId == id).SingleOrDefault();
  27. return View(course);
  28. }
  29. #endregion
  30. #region Create course
  31. public ActionResult Create()
  32. {
  33. return View(new Course());
  34. }
  35. [HttpPost]
  36. public ActionResult Create(Course course)
  37. {
  38. objContext.Courses.Add(course);
  39. objContext.SaveChanges();
  40. return RedirectToAction("Index");
  41. }
  42. #endregion
  43. #region edit course
  44. public ActionResult Edit(int id)
  45. {
  46. Course course = objContext.Courses.Where(x => x.CourseId == id).SingleOrDefault();
  47. return View(course);
  48. }
  49. [HttpPost]
  50. public ActionResult Edit(Course model)
  51. {
  52. Course course = objContext.Courses.Where(x => x.CourseId == model.CourseId).SingleOrDefault();
  53. if (course != null)
  54. {
  55. objContext.Entry(course).CurrentValues.SetValues(model);
  56. objContext.SaveChanges();
  57. return RedirectToAction("Index");
  58. }
  59. return View(model);
  60. }
  61. #endregion
  62. #region Delete course
  63. public ActionResult Delete(int id)
  64. {
  65. Course course = objContext.Courses.Find(id);
  66. return View(course);
  67. }
  68. [HttpPost]
  69. public ActionResult Delete(int id, Course model)
  70. {
  71. var course = objContext.Courses.Where(x => x.CourseId == id).SingleOrDefault();
  72. if (course != null)
  73. {
  74. objContext.Courses.Remove(course);
  75. objContext.SaveChanges();
  76. }
  77. return RedirectToAction("Index");
  78. }
  79. #endregion
  80. }
  81. }
View for Course

Index.cshtml
  1. @model IEnumerable<CodeFirstApproachWithPrimaryForeignKey.Models.Course>
  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.CourseName)
  13. </th>
  14. <th></th>
  15. </tr>
  16. @foreach (var item in Model) {
  17. <tr>
  18. <td>
  19. @Html.DisplayFor(modelItem => item.CourseName)
  20. </td>
  21. <td>
  22. @Html.ActionLink("Edit", "Edit", new { id=item.CourseId }) |
  23. @Html.ActionLink("Details", "Details", new { id=item.CourseId }) |
  24. @Html.ActionLink("Delete", "Delete", new { id=item.CourseId })
  25. </td>
  26. </tr>
  27. }
  28. </table>
Create.cshtml
  1. @model CodeFirstApproachWithPrimaryForeignKey.Models.Course
  2. @{
  3. ViewBag.Title = "Create";
  4. }
  5. <h2>Create</h2>
  6. @using (Html.BeginForm()) {
  7. @Html.ValidationSummary(true)
  8. <fieldset>
  9. <legend>Course</legend>
  10. <div class="editor-label">
  11. @Html.LabelFor(model => model.CourseName)
  12. </div>
  13. <div class="editor-field">
  14. @Html.EditorFor(model => model.CourseName)
  15. @Html.ValidationMessageFor(model => model.CourseName)
  16. </div>
  17. <p>
  18. <input type="submit" value="Create" />
  19. </p>
  20. </fieldset>
  21. }
  22. <div>
  23. @Html.ActionLink("Back to List", "Index")
  24. </div>
  25. @section Scripts {
  26. @Scripts.Render("~/bundles/jqueryval")
  27. }
Delete.cshtml
  1. model CodeFirstApproachWithPrimaryForeignKey.Models.Course
  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>Course</legend>
  9. <div class="display-label">
  10. @Html.DisplayNameFor(model => model.CourseName)
  11. </div>
  12. <div class="display-field">
  13. @Html.DisplayFor(model => model.CourseName)
  14. </div>
  15. </fieldset>
  16. @using (Html.BeginForm()) {
  17. <p>
  18. <input type="submit" value="Delete" /> |
  19. @Html.ActionLink("Back to List", "Index")
  20. </p>
  21. }
Details.cshtml
  1. @model CodeFirstApproachWithPrimaryForeignKey.Models.Course
  2. @{
  3. ViewBag.Title = "Details";
  4. }
  5. <h2>Details</h2>
  6. <fieldset>
  7. <legend>Course</legend>
  8. <div class="display-label">
  9. @Html.DisplayNameFor(model => model.CourseName)
  10. </div>
  11. <div class="display-field">
  12. @Html.DisplayFor(model => model.CourseName)
  13. </div>
  14. </fieldset>
  15. <p>
  16. @Html.ActionLink("Edit", "Edit", new { id=Model.CourseId }) |
  17. @Html.ActionLink("Back to List", "Index")
  18. </p>
Edit.cshtml
  1. @model CodeFirstApproachWithPrimaryForeignKey.Models.Course
  2. @{
  3. ViewBag.Title = "Edit";
  4. }
  5. <h2>Edit</h2>
  6. @using (Html.BeginForm()) {
  7. @Html.ValidationSummary(true)
  8. <fieldset>
  9. <legend>Course</legend>
  10. @Html.HiddenFor(model => model.CourseId)
  11. <div class="editor-label">
  12. @Html.LabelFor(model => model.CourseName)
  13. </div>
  14. <div class="editor-field">
  15. @Html.EditorFor(model => model.CourseName)
  16. @Html.ValidationMessageFor(model => model.CourseName)
  17. </div>
  18. <p>
  19. <input type="submit" value="Save" />
  20. </p>
  21. </fieldset>
  22. }
  23. <div>
  24. @Html.ActionLink("Back to List", "Index")
  25. </div>
  26. @section Scripts {
  27. @Scripts.Render("~/bundles/jqueryval")
  28. }
Run the Application

create

Now see your database:

database

Click on Course then select Create New.

create new

Showing All Course List:

course list

Click on Edit Course:

edit course

Details of a course:

course detail

Delete an existing Course:

delete course

Now click on Student-> Create New.

student create new

List of all students.

student list

Edit any student record:

edit student

Details of any student:

student detail

Delete any student record:

delete student

Now see your database. See the records in both tables and see the Primary Key and Foreign Key existence:

check database

If you want to run this application in your machine then just change the connection string in the web.config file.