Look at the following procedure.

Step 1

Create a database with some sample data using the following SQL script.

  1. CREATE DATABASE MVC;
  2. USE MVC;
  3. CREATE TABLE dbo.Students(ID int, Name varchar(50), Gender varchar(6), Fees int);
  4. INSERT INTO dbo.Students
  5. VALUES(1, 'Harry', 'Male', 2500);
  6. INSERT INTO dbo.Students
  7. VALUES(2, 'Jane', 'Female', 2400);
  8. INSERT INTO dbo.Students
  9. VALUES(3, 'Emma', 'Female', 2100);
  10. INSERT INTO dbo.Students
  11. VALUES(4, 'Roster', 'Male', 2500);
  12. INSERT INTO dbo.Students
  13. VALUES(5, 'Chris', 'Male', 2900);
  14. INSERT INTO dbo.Students
  15. VALUES(6, 'Evan', 'Male', 2200);
  16. INSERT INTO dbo.Students
  17. VALUES(7, 'Cathlie', 'Female', 2550);
  18. INSERT INTO dbo.Students
  19. VALUES(8, 'Jack', 'Male', 2500);
  20. INSERT INTO dbo.Students
  21. VALUES(9, 'Jone', 'Male', 2900);
  22. INSERT INTO dbo.Students
  23. VALUES(10, 'Videra', 'Female', 2550);
  24. INSERT INTO dbo.Students
  25. VALUES(11, 'Sara', 'Female', 2900);
  26. INSERT INTO dbo.Students
  27. VALUES(12, 'Mak', 'Male', 2500);
  28. INSERT INTO dbo.Students
  29. VALUES(13, 'Max', 'Male', 2550);
  30. INSERT INTO dbo.Students
  31. VALUES(14, 'Brock', 'Male', 2900);
  32. INSERT INTO dbo.Students
  33. VALUES(15, 'Eddie', 'Male', 2500);
  34. INSERT INTO dbo.Students
  35. VALUES(16, 'Edna', 'Female', 2500);
Step 2

Create a new MVC Project in Visual Studio and name it "MVCDataAccessByEntityFrame".

MVC Project

empty templet

Step 3

Install Entity Framework using Nuget package manager from Solution Explorer into your project.

nuget package manager

solution explorer

Step 4

Go to the Models folder in the Solution Explorer and add two class files.

Models

Step 5

Copy the following code to the preceding created class files.

Students.cs
  1. using System.ComponentModel.DataAnnotations.Schema;
  2. namespace MVCDataAccessByEntityFrame.Models
  3. {
  4. [Table("Students")]
  5. public class Students
  6. {
  7. public int ID { get; set; }
  8. public string Name { get; set; }
  9. public string Gender { get; set; }
  10. public int Fees { get; set; }
  11. }
  12. }
StudentsContext.cs
  1. using System.Data.Entity;
  2. namespace MVCDataAccessByEntityFrame.Models
  3. {
  4. public class StudentsContext : DbContext
  5. {
  6. public DbSet<Students> Students { get; set; }
  7. }
  8. }
Step 6

Go to the Controllers folder and add a controller to it.

Controllers

Step 7

Add the following code to the controller.
  1. using MVCDataAccessByEntityFrame.Models;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web.Mvc;
  5. namespace MVCDataAccessByEntityFrame.Controllers
  6. {
  7. public class StudentsController : Controller
  8. {
  9. public ActionResult Index()
  10. {
  11. StudentsContext studentsContext = new StudentsContext();
  12. List<Students> students = studentsContext.Students.ToList();
  13. return View(students);
  14. }
  15. public ActionResult Details(int id)
  16. {
  17. StudentsContext studentsContext = new StudentsContext();
  18. Students students = studentsContext.Students.Single(stu => stu.ID == id);
  19. return View(students);
  20. }
  21. }
  22. }
Step 8

Press Ctrl + Shift + B and then right-click on the index method and add a view.

index method

Step 9

Add the following code to the Index.cshtml.
  1. @model IEnumerable<MVCDataAccessByEntityFrame.Models.Students>
  2. @using MVCDataAccessByEntityFrame.Models;
  3. @{
  4. ViewBag.Title = "Students List";
  5. }
  6. <h2>Students List</h2>
  7. <ol start="1">
  8. @foreach (Students students in @Model)
  9. {
  10. <li id="item">
  11. @Html.ActionLink(students.Name, "Details", new { id = students.ID })
  12. </li>
  13. }
  14. </ol>
Step 10

Right-click on the Details method and add a view again.

add a view

Step 11

Add the following code to details.cshtml.
  1. @model MVCDataAccessByEntityFrame.Models.Students
  2. @{
  3. ViewBag.Title = "Students Details";
  4. }
  5. <table border="1">
  6. <tr>
  7. <td><b>ID:</b></td>
  8. <td>
  9. @Model.ID
  10. </td>
  11. </tr>
  12. <tr>
  13. <td><b>Name:</b></td>
  14. <td>@Model.Name</td>
  15. </tr>
  16. <tr>
  17. <td><b>Gender:</b></td>
  18. <td>@Model.Gender</td>
  19. </tr>
  20. <tr>
  21. <td><b>Fees:</b></td>
  22. <td>@Model.Fees</td>
  23. </tr>
  24. </table>
  25. <br />
  26. @Html.ActionLink("Back to Students List", "Index")
Step 12

Go to the web.config file in the Views folder in Solution Explorer and add the following code.
  1. <connectionStrings>
  2. <add name="StudentsContext" connectionString="server=ANKITBANSALPC; database = MVC; integrated security = SSPI" providerName="System.Data.SqlClient"/>
  3. </connectionStrings>
Step 13

Go to the Route.config file in the App_Start folder and paste in the following code.
  1. using System.Web.Mvc;
  2. using System.Web.Routing;
  3. namespace MVCDataAccessByEntityFrame
  4. {
  5. public class RouteConfig
  6. {
  7. public static void RegisterRoutes(RouteCollection routes)
  8. {
  9. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  10. routes.MapRoute(
  11. name: "Default",
  12. url: "{controller}/{action}/{id}",
  13. defaults: new { controller = "Students", action = "Index", id = UrlParameter.Optional }
  14. );
  15. }
  16. }
  17. }
Step 14

Save all the changes and Press F5 to run the project and you will see a list of Students and on clicking a specific student name, you will see the details of that student.

student list

details

Explanation

Students.cs

StudentsContext.cs

StudentsController.cs

Index.cshtml

Details.cshtml

Route.config

Download Source code here.

See this article in my own blog: debugsolutions and technewsinform.