To run your SQL code, follow these steps:
Simple:
Copy SQL → Open SSMS → New Query → Paste → Execute.
-- Create Database CREATE DATABASE MVC_StudentDB GO -- Use Database USE MVC_StudentDB GO -- Create Student Table CREATE TABLE Student ( StudentId INT PRIMARY KEY IDENTITY(1,1), Name NVARCHAR(50) NOT NULL, Age INT NOT NULL, Gender NVARCHAR(10) NOT NULL, Course NVARCHAR(50) NOT NULL, Marks INT NOT NULL ) GO -- Insert Default Data INSERT INTO Student (Name, Age, Gender, Course, Marks) VALUES ('Ali', 20, 'Male', 'BCA', 85), ('Sara', 22, 'Female', 'BBA', 90), ('Ahmed', 21, 'Male', 'BSc IT', 78), ('Fatima', 23, 'Female', 'MBA', 88), ('Khushbu', 20, 'Female', 'BSC', 99) GO
After opening Visual Studio 2022, click Create a new project.
Select Empty Template
Project Created Successfully:
Now, to add the database model: Right-click on your Project Name → Add → New Item → Select Data → ADO.NET Entity Data Model → Click Add.
Right-click on your Project Name
→ Click Add
→ Click New Item...
- EF Designer from database (to generate model from existing database)
In the Entity Data Model Wizard, after creating the database connection:
In the Entity Data Model Wizard, when selecting the Entity Framework version:
This lets the wizard know which version of EF to use for generating your model.
In the Entity Data Model Wizard, after selecting the Entity Framework version:
Once finished, Visual Studio will create the Entity Framework model with your selected tables inside the project.
Simple:
Select tables → Enter Model Namespace "MVC_StudentDBModel" → Click Finish → Model created.
After completing the Entity Data Model wizard and clicking Finish:
Your Entity Framework model has been created successfully inside the project. For example, the Student table now appears in the Model1.edmx diagram.
This means your database table is now connected to your project through Entity Framework. You can now use this model in your Controllers to fetch or update data.
Simple:
After Finish → Student table appears in Model1.edmx → Columns & Keys are ready → Can use in Controller.
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace Your_Project_Name { using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; public partial class MVC_StudentDBEntities : DbContext { public MVC_StudentDBEntities() : base("name=MVC_StudentDBEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<Student> Students { get; set; } } }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace Your_Project_Name { using System; using System.Collections.Generic; public partial class Student { public int StudentId { get; set; } public string Name { get; set; } public int Age { get; set; } public string Gender { get; set; } public string Course { get; set; } public int Marks { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace Your_Project_Name { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Student",action = "Index",id = UrlParameter.Optional } ); } } }
Now, create a new controller for the Student.
Go to the Controllers folder 📁, right-click on it, and select Add → Controller.
Choose MVC 5 Controller – Empty and click Add.
Name the controller StudentController and click Add.
After creating the controller, open StudentController.cs and update it with the code provided.
Simple:
Controllers 📁 → Right Click → Add Controller →
Name it StudentController →
Open file → Update the code.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Your_Project_Name.Models; namespace Your_Project_Name.Controllers { public class StudentController : Controller { // GET: Student public ActionResult Index() { MVC_StudentDBEntities dbContext = new MVC_StudentDBEntities(); List<Student> StuList = dbContext.Students.ToList(); return View(StuList); } } }
After creating the StudentController, open the controller file and right-click on the Index action method. Select Add View.
In the Add View window, configure the following options:
Click the Add button. Visual Studio will automatically generate a strongly typed MVC 5 View that displays the list of students.
If the view is not generated correctly, you may copy the provided code and update the Index.cshtml file manually.
Summary:
StudentController → Right Click Index → Add View →
Select List Template → Choose Student Model →
Select MVC_StudentDBEntities → Click Add.
Index.cshtml
@model IEnumerable<Your_Project_Name.Student> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table class="table"> <tr> <th>@Html.DisplayNameFor(model => model.Name)</th> <th>@Html.DisplayNameFor(model => model.Age)</th> <th>@Html.DisplayNameFor(model => model.Gender)</th> <th>@Html.DisplayNameFor(model => model.Course)</th> <th>@Html.DisplayNameFor(model => model.Marks)</th> <th></th> </tr> @foreach (var item in Model) { <tr> <td>@Html.DisplayFor(modelItem => item.Name)</td> <td>@Html.DisplayFor(modelItem => item.Age)</td> <td>@Html.DisplayFor(modelItem => item.Gender)</td> <td>@Html.DisplayFor(modelItem => item.Course)</td> <td>@Html.DisplayFor(modelItem => item.Marks)</td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.StudentId }) | @Html.ActionLink("Details", "Details", new { id=item.StudentId }) | @Html.ActionLink("Delete", "Delete", new { id=item.StudentId }) </td> </tr> } </table>
Output
https://localhost:44369/Student/Index