Here with Entity Framework we can develop an application using any of the following 3 approaches.
- Code First
- Model First
- 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.
- 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; }
- }
- }
Now again right-click on the Model Folder and select Add New Class.
- 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);
- }
- }
- }
Here I am using a connection string. Be sure you define it in your web.config file.
- <add name="DbConnectionString"
- connectionString="Data Source=INDIA\MSSQLServer2k8;Initial Catalog=MyCodeFirstAPPDB;User ID=sa; Password=india;"
- 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.
- 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
- {
-
-
-
- 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
-
- }
- }
Now My Views are:
Index.cshtml
- @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>
Create.cshtml
- @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")
- }
Details.cshtml
- @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>
- }
Edit.cshtml
- @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")
- }
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.