Featured Article

ASP NET Entity Framework Code First Example Tutorial For Beginners

Kenneth Jul 13, 2026

As an application developer, the need to interact with databases is a given. This is where the .NET Entity Framework comes in - a powerful object-relational mapping (ORM) library that enables .NET developers to work with databases using .NET objects and collections instead of SQL queries. Among its various approaches, Entity Framework Code First is a popular method that allows developers to create their database schema based on simple POCO (Plain Old CLR Objects) classes. Let's dive into an example of how to use ASP.NET Core with Entity Framework Code First.

#dotnet #entityframework #efcore #aspnetcore #backenddevelopment #softwareengineering #webdevelopment #programmingconcepts #techlearning #developercommunity | Sagar Saini
#dotnet #entityframework #efcore #aspnetcore #backenddevelopment #softwareengineering #webdevelopment #programmingconcepts #techlearning #developercommunity | Sagar Saini

Before we start, ensure you have the latest .NET SDK and Visual Studio installed. For this example, we'll create a simple blog with posts and comments using ASP.NET Core MVC.

Data Access in ASP.NET Core using EF Core (Code First)
Data Access in ASP.NET Core using EF Core (Code First)

Setting Up Your ASP.NET Core Project

First, create a new ASP.NET Core MVC project. In your terminal, run:

Free Entity Framework Book
Free Entity Framework Book

dotnet new mvc -n BlogExample

Installing Required Packages

Database First Approach of Entity Framework in Asp.Net MVC 4 Example - Tutlane
Database First Approach of Entity Framework in Asp.Net MVC 4 Example - Tutlane

Let's install the required packages: Microsoft.EntityFrameworkCore.Sqlite (for the SQLite database engine), and Microsoft.EntityFrameworkCore.Design (for the EF Core tools).

Run this command in your project folder to install them: dotnet add package Microsoft.EntityFrameworkCore.Sqlite Microsoft.EntityFrameworkCore.Design

Creating Your Models

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

We'll create two models: Post.cs and Comment.cs. In your project root, create a new folder named Data, and inside it, a new class named Models.

First, create the Post model:

public class Post { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public List<Comment> Comments { get; set; } = new List<Comment>(); }

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

Now, create the Comment model:

public class Comment { public int Id { get; set; } public string Name { get; set; } public string Content { get; set; } public int PostId { get; set; } public Post Post { get; set; } }

ASP.Net Projects with Source Code
ASP.Net Projects with Source Code
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
What is the difference between ASP.NET and ASP.NET Core?
What is the difference between ASP.NET and ASP.NET Core?
Detailed ASP.NET MVC Pipeline
Detailed ASP.NET MVC Pipeline
Code First Approach using Entity Framework 4.0 Sample example using ASP.NET,C#.NET
Code First Approach using Entity Framework 4.0 Sample example using ASP.NET,C#.NET
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
Data Access in ASP.NET Core using EF Core (Database First)
Data Access in ASP.NET Core using EF Core (Database First)
Web Application, 10 Things
Web Application, 10 Things
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

Configuring the DbContext

Next, we need to create a DbContext class to manage our database. In the Data folder, create a new class named ApplicationDbContext:

public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<Post> Posts { get; set; } public DbSet<Comment> Comments { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Post>().HasMany(p => p.Comments).WithOne(c => c.Post).HasForeignKey(c => c.PostId); } }

Registering the DbContext

Now, in your Startup.cs file, register the DbContext in the ConfigureServices method:

services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

Creating the Database

To create the database using Code First, we'll add a new migration and update the database:

dotnet ef migrations add InitialCreate

dotnet ef database update

Creating Controllers and ViewModels

Now, create PostController.cs and CommentController.cs in the Controllers folder. Implement CRUD operations for posts and comments, using the DbContext to interact with the database. Also, create corresponding view models and views in the Views/Shared folder.

With these steps, you've created a simple ASP.NET Core MVC application using Entity Framework Code First to manage your database. This approach allows you to focus on your models and lets Entity Framework take care of the database schema. Happy coding!

Now that you have a solid grasp of ASP.NET Core MVC with Entity Framework Code First, consider exploring more of its features, likeigrations, and queries. And remember, keeping your skills up-to-date is key in the ever-evolving world of software development. Happy coding!