Featured Article

ASP NET MVC Entity Framework Code First Approach Example Tutorial

Kenneth Jul 13, 2026

ASP.NET MVC with Entity Framework (EF) is a powerful combination for building modern web applications. The 'Code First' approach allows you to define your models first, then let EF create the database schema based on those models. Let's dive into an example to understand this approach better.

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

The beauty of Code First lies in its simplicity and flexibility. It allows you to define your database schema in code, providing more control than the traditional Database First or Model First approaches.

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 the Environment

First, ensure you have the necessary tools installed: Visual Studio (with the 'ASP.NET and Web Development' workload), .NET Core SDK, and an Ole DB data provider like Microsoft.EntityFrameworkCore.SqlServer.

Free Entity Framework Book
Free Entity Framework Book

Create a new ASP.NET Core MVC project with the Individual User Accounts authentication. This will provide us with a basic application structure andIdentity user management out of the box.

Defining the Model

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

droit;'>In the 'Models' folder, create a new class called 'ApplicationUser.cs'. This will represent our custom user model, which extends the default IdentityUser class.

```csharp public class ApplicationUser : IdentityUser { public string FirstName { get; set; } = null!; public string LastName { get; set; } = null!; } ```

Creating the DbContext

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

Next, create a new class called 'ApplicationDbContext.cs' in the 'Data' folder. This class will derive from DbContext and configure Entity Framework.

```csharp using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } } ```

Using the Models and DbContext

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

Now that we've defined our model and DbContext, it's time to use them in our application.

The Identity layer in ASP.NET Core uses our ApplicationDbContext for user management. You can utilize the ApplicationUser model in your controllers or views as needed.

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
Introduction to Entity Framework Core - The Engineering Projects
Introduction to Entity Framework Core - The Engineering Projects
.Net Framework
.Net Framework
ASP.Net Projects with Source Code
ASP.Net Projects with Source Code
How to use Master Page in Asp.net
How to use Master Page in Asp.net
An introduction to OpenID Connect in ASP.NET Core
An introduction to OpenID Connect in ASP.NET Core
Post by @im-a-developer · 1 image
Post by @im-a-developer · 1 image
a diagram showing the different types of vcp's and their respective service areas
a diagram showing the different types of vcp's and their respective service areas
GitHub - MoienTajik/AspNetCore-Developer-Roadmap: Roadmap to becoming an ASP.NET Core developer in 2026
GitHub - MoienTajik/AspNetCore-Developer-Roadmap: Roadmap to becoming an ASP.NET Core developer in 2026

Registering the DbContext

In the 'Startup.cs' file, configure the dependency injection for our ApplicationDbContext. This allows us to use it in our controllers or services.

```csharp public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); // ... } ```

Using the DbContext in Controllers

You can inject our ApplicationDbContext into a controller to perform database operations. Here's an example of using it to query users:

```csharp public class HomeController : Controller { private readonly ApplicationDbContext _context; public HomeController(ApplicationDbContext context) { _context = context; } public IActionResult Index() { var users = _context.Users.ToList(); return View(users); } } ```

With this setup, you have a functional ASP.NET MVC application using Entity Framework Code First. You can further expand this example to include data manipulation (Create, Read, Update, Delete) operations and more complex queries.

Exploring and understanding Code First allows you to build flexible and maintainable applications. It's a powerful approach that encourages a clean separation of concerns, making your code easier to test and change over time. Happy coding!