Featured Article

Complete Step by Step ASP NET MVC 5 Entity Framework 6 Database First Tutorial Guide

Kenneth Jul 13, 2026

Embarking on your ASP.NET MVC 5 and Entity Framework 6 journey? The Database First approach streamlines the process of generating models based on your existing database schema. This step-by-step tutorial will guide you through this process, optimizing your learning experience and providing SEO-friendly insights along the way.

the architecture diagram for an application
the architecture diagram for an application

Before we dive in, ensure you have the following pre-requisites: Visual Studio 2013 or above, a basic understanding of ASP.NET MVC, and an existing SQL database ready to be consumed.

Code First Approach in Entity Framework in Asp.net MVC with Example - Tutlane
Code First Approach in Entity Framework in Asp.net MVC with Example - Tutlane

Setting Up Your ASP.NET MVC 5 Project

Let's kickstart by creating a new MVC project with Entity Framework support.

ASP.NET and MVC Tutorial Guide
ASP.NET and MVC Tutorial Guide

1. Open Visual Studio and select 'New Project'. Choose 'Web' then 'ASP.NET Web Application'. Name it, choose a location, and click 'OK'.

Enabling Entity Framework

Free Entity Framework Book
Free Entity Framework Book

In the 'New ASP.NET Project' window, select 'MVC' and make sure 'Entity Framework' is checked. Click 'Change Authentication' then 'Create'.

You'll notice the addition of a 'Models' folder and relevant NuGet packages in your solution.

Connecting to Your Database

Detailed ASP.NET MVC Pipeline
Detailed ASP.NET MVC Pipeline

Right-click on your project in Solution Explorer, select 'Add' then 'New Item'. Choose 'ADO.NET Entity Data Model' and name it (e.g., 'YourModel.edmx').

Click 'Add' to create a new Entity Data Model wizard. Click 'Next', select 'Database', and click 'Next' again. Click 'New Connection...', select your database, and test the connection. Click 'OK', select the tables you want to include, and click 'Finish'.

Generating Your Models

Folder Structure in Asp.Net MVC Project (Application) - Tutlane
Folder Structure in Asp.Net MVC Project (Application) - Tutlane

Now that your EDMX file is populated with tables, let's generate the underlying C# models.

Right-click on the 'Models' folder, select 'Add' then 'New Item'. Choose 'Code First from DB Context' and name it (e.g., 'YourModel.cs').

Introduction to Entity Framework Core - The Engineering Projects
Introduction to Entity Framework Core - The Engineering Projects
the asp net core info sheet shows what it is like to work on an application
the asp net core info sheet shows what it is like to work on an application
ASP.NET MVC with Entity Framework and CSS
ASP.NET MVC with Entity Framework and CSS
CRUD Operations using ADO.Net Entity Framework in Asp.Net MVC Example - Tutlane
CRUD Operations using ADO.Net Entity Framework in Asp.Net MVC Example - Tutlane
ASP.Net Projects with Source Code
ASP.Net Projects with Source Code
How to: Access Nested List View or Master Detail View Environment (ASP.NET Core Blazor and Windows Forms) | XAF: Cross-Platform .NET App UI & Web API
How to: Access Nested List View or Master Detail View Environment (ASP.NET Core Blazor and Windows Forms) | XAF: Cross-Platform .NET App UI & Web API
A Step by Step Guide for ASP.NET Core Configuration
A Step by Step Guide for ASP.NET Core Configuration
.Net Framework
.Net Framework
an image of a computer screen with text and numbers on it, including the same language as
an image of a computer screen with text and numbers on it, including the same language as

Creating the DbContext Class

Right-click in your 'YourModel.cs' file, select 'Add' then 'New Item'. Choose 'Context' and name it (e.g., 'YourModelContext.cs').

In 'YourModelContext.cs', replace the contents with the following, adjusting the 'DbSet' properties as needed:

```csharp public partial class YourModelContext : DbContext { public YourModelContext() : base("name=YourModelContext") { } public virtual DbSet YourTable { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.CurrentModel.EntityTypes.AddRange( new DbEntityType[] { modelBuilder.BuildType(typeof(YourTable)) .EntityType }); base.OnModelCreating(modelBuilder); } } ```

In 'YourModelipso.cs', remove generated classes and the 'db' variable. Now, your models are ready to use in your controller methods.

Creating Controller and Views

Right-click on the 'Controllers' folder, select 'Add' then 'Controller' and choose 'MVC Controller with views' using your model and context. Name it (e.g., 'YourTableController.cs') and click 'Add'.

This will create your controller, index, details, create, edit, and delete views. You can now interact with your database using Entity Framework 6 through your ASP.NET MVC 5 application.

Remember to regex optimize your URL structures for SEO, and regularly commit your code to maintain a clean version history. Happy coding!