This scenario targets a database that doesn’t exist and code first will create, or an empty database that code first will add new tables .

Note: Make sure that the Entity Framework already installed with Visual Studio, otherwise install using NuGet Packages.

Step 1. Creating an MVC Application

Step 2. Creating the Model

Right click on the Models folder and create a model with the name of "User" as in the following image and create a property as in the below code,

solution

  1. public class User
  2. {
  3. public int UserId
  4. {
  5. get;
  6. set;
  7. }
  8. public string FirstName
  9. {
  10. get;
  11. set;
  12. }
  13. public string LastName
  14. {
  15. get;
  16. set;
  17. }
  18. public string EMail
  19. {
  20. get;
  21. set;
  22. }
  23. public string Address
  24. {
  25. get;
  26. set;
  27. }
  28. public string PhoneNo
  29. {
  30. get;
  31. set;
  32. }
  33. public string Company
  34. {
  35. get;
  36. set;
  37. }
  38. public string Designation
  39. {
  40. get;
  41. set;
  42. }
  43. }
Step 3. Create a DBContext

Create a class with the Name MVCDBContext as in the following screenshot and write the code as follows in MVCDBContext class.

solution

  1. using System.Data.Entity;
  2. using System.Linq;
  3. using CodeFirstDemo.Models;
  4. namespace CodeFirstDemo
  5. {
  6. public class MVCDBContext: DbContext
  7. {
  8. public DbSet < User > Users
  9. {
  10. get;
  11. set;
  12. }
  13. }
  14. }
[ Note : Make sure that you are using " System.Data.Entity " Namespace. ]

Step 4 : Create a Controller.

Right click on the Controller folder and create a controller with the name "MyController" as in the following image. Write the following code in Mycontroller.cs within Index Action.

solution

  1. public ActionResult Index()
  2. {
  3. var dbContext = new MVCDBContext();
  4. var userList = from user in dbContext.Users select user;
  5. var users = new List < CodeFirstDemo.Models.User > ();
  6. if (userList.Any())
  7. {
  8. foreach(var user in userList)
  9. {
  10. users.Add(new CodeFirstDemo.Models.User()
  11. {
  12. UserId = user.UserId, Address = user.Address,
  13. Company = user.Company, FirstName = user.FirstName,
  14. LastName = user.LastName, Designation = user.Designation,
  15. EMail = user.EMail, PhoneNo = user.PhoneNo
  16. });
  17. }
  18. }
  19. ViewBag.FirstName = "My First Name";
  20. ViewData["FirstName"] = "My First Name";
  21. if (TempData.Any())
  22. {
  23. var tempData = TempData["TempData Name"];
  24. }
  25. return View(users);
  26. }
Now ,

Create.cshtml

  1. @model CodeFirstDemo.Models.User
  2. @
  3. {
  4. ViewBag.Title = "Create";
  5. } < h2 > Create < /h2> < script src = "@Url.Content("~/Scripts/jquery.validate.min.js ")"
  6. type = "text/javascript" > < /script> < script src = "@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js ")"
  7. type = "text/javascript" > < /script>
  8. @using(Html.BeginForm())
  9. {
  10. @Html.ValidationSummary(true) < fieldset > < legend > User
  11. < /legend> < div class = "editor-label" >
  12. @Html.LabelFor(model => model.FirstName)
  13. < /div> < div class = "editor-field" >
  14. @Html.EditorFor(model => model.FirstName)
  15. @Html.ValidationMessageFor(model => model.FirstName)
  16. < /div> < div class = "editor-label" >
  17. @Html.LabelFor(model => model.LastName)
  18. < /div> < div class = "editor-field" >
  19. @Html.EditorFor(model => model.LastName)
  20. @Html.ValidationMessageFor(model => model.LastName)
  21. < /div> < div class = "editor-label" >
  22. @Html.LabelFor(model => model.EMail)
  23. < /div> < div class = "editor-field" >
  24. @Html.EditorFor(model => model.EMail)
  25. @Html.ValidationMessageFor(model => model.EMail)
  26. < /div> < div class = "editor-label" >
  27. @Html.LabelFor(model => model.Address)
  28. < /div> < div class = "editor-field" >
  29. @Html.EditorFor(model => model.Address)
  30. @Html.ValidationMessageFor(model => model.Address)
  31. < /div> < div class = "editor-label" >
  32. @Html.LabelFor(model => model.PhoneNo)
  33. < /div> < div class = "editor-field" >
  34. @Html.EditorFor(model => model.PhoneNo)
  35. @Html.ValidationMessageFor(model => model.PhoneNo)
  36. < /div> < div class = "editor-label" >
  37. @Html.LabelFor(model => model.Company)
  38. < /div> < div class = "editor-field" >
  39. @Html.EditorFor(model => model.Company)
  40. @Html.ValidationMessageFor(model => model.Company)
  41. < /div> < div class = "editor-label" >
  42. @Html.LabelFor(model => model.Designation)
  43. < /div> < div class = "editor-field" >
  44. @Html.EditorFor(model => model.Designation)
  45. @Html.ValidationMessageFor(model => model.Designation)
  46. < /div> < p > < input type = "submit"
  47. value = "Create" / > < /p> < /fieldset>
  48. } < div > @Html.ActionLink("Back to List", "Index")
  49. < /div>
Delete.cshtml
  1. @model CodeFirstDemo.Models.User
  2. @
  3. {
  4. ViewBag.Title = "Delete";
  5. }
  6. < h2 > Delete
  7. < /h2>
  8. < h3 > Are you sure you want to delete this ?
  9. < /h3> < fieldset > < legend > User
  10. < /legend>
  11. < div class = "display-label" > FirstName
  12. < /div> < div class = "display-field" >
  13. @Html.DisplayFor(model => model.FirstName)
  14. < /div> < div class = "display-label" > LastName
  15. < /div> < div class = "display-field" >
  16. Html.DisplayFor(model => model.LastName)
  17. < /div> < div class = "display-label" > EMail
  18. < /div> < div class = "display-field" >
  19. @Html.DisplayFor(model => model.EMail)
  20. < /div> < div class = "display-label" > Address
  21. < /div> < div class = "display-field" >
  22. @Html.DisplayFor(model => model.Address)
  23. < /div> < div class = "display-label" > PhoneNo
  24. < /div> < div class = "display-field" >
  25. @Html.DisplayFor(model => model.PhoneNo)
  26. < /div> < div class = "display-label" > Company
  27. < /div> < div class = "display-field" >
  28. @Html.DisplayFor(model => model.Company)
  29. < /div> < div class = "display-label" > Designation
  30. < /div> < div class = "display-field" >
  31. @Html.DisplayFor(model => model.Designation)
  32. < /div> < /fieldset>
  33. @using(Html.BeginForm())
  34. { < p > < input type = "submit"
  35. value = "Delete" / > | @Html.ActionLink("Back to List", "Index") < /p>
  36. }
Edit.cshtml
  1. @model CodeFirstDemo.Models.User
  2. @
  3. {
  4. ViewBag.Title = "Edit";
  5. } < h2 > Edit < /h2> < script src = "@Url.Content("~/Scripts/jquery.validate.min.js ")"
  6. type = "text/javascript" > < /script> < script src = "@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js ")"
  7. type = "text/javascript" > < /script>
  8. @using(Html.BeginForm())
  9. {
  10. @Html.ValidationSummary(true) < fieldset > < legend > User < /legend>
  11. @Html.HiddenFor(model => model.UserId) < div class = "editor-label" > @Html.LabelFor(model => model.FirstName) < /div> < div class = "editor-field" > @Html.EditorFor(model => model.FirstName)
  12. @Html.ValidationMessageFor(model => model.FirstName)
  13. < /div> < div class = "editor-label" >
  14. @Html.LabelFor(model => model.LastName)
  15. < /div> < div class = "editor-field" >
  16. @Html.EditorFor(model => model.LastName)
  17. @Html.ValidationMessageFor(model => model.LastName)
  18. < /div> < div class = "editor-label" >
  19. @Html.LabelFor(model => model.EMail)
  20. < /div> < div class = "editor-field" >
  21. @Html.EditorFor(model => model.EMail)
  22. @Html.ValidationMessageFor(model => model.EMail)
  23. < /div> < div class = "editor-label" >
  24. @Html.LabelFor(model => model.Address)
  25. < /div> < div class = "editor-field" >
  26. @Html.EditorFor(model => model.Address)
  27. @Html.ValidationMessageFor(model => model.Address)
  28. < /div> < div class = "editor-label" >
  29. @Html.LabelFor(model => model.PhoneNo)
  30. < /div> < div class = "editor-field" >
  31. @Html.EditorFor(model => model.PhoneNo)
  32. @Html.ValidationMessageFor(model => model.PhoneNo)
  33. < /div> < div class = "editor-label" >
  34. @Html.LabelFor(model => model.Company)
  35. < /div> < div class = "editor-field" >
  36. @Html.EditorFor(model => model.Company)
  37. @Html.ValidationMessageFor(model => model.Company)
  38. < /div> < div class = "editor-label" >
  39. @Html.LabelFor(model => model.Designation)
  40. < /div> < div class = "editor-field" >
  41. @Html.EditorFor(model => model.Designation)
  42. @Html.ValidationMessageFor(model => model.Designation)
  43. < /div> < p > < input type = "submit"
  44. value = "Save" / > < /p> < /fieldset>
  45. } < div > @Html.ActionLink("Back to List", "Index") < /div>
Please make sure your Web.config file looks like the following:

config

Now press F5 to run the application,

Summary

In this walk-through we looked at code first development using a new database. We defined a model using classes and then used that model to create a database and performed CRUD operation.