Featured Article

Asp Net Core Entity Framework Code First Example Tutorial

Kenneth Jul 13, 2026

Hello, developers! Today, we're diving into the world of ASP .NET Core and Entity Framework Core, focusing on the Code First approach. If you're new to these technologies or looking to expand your knowledge, you've come to the right place.

Free Entity Framework Book
Free Entity Framework Book

ASP .NET Core is a powerful, high-performance, open-source framework for building modern, cloud-based, Internet-connected applications. Entity Framework Core, on the other hand, is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology. When combined, they form a robust stack for building data-driven web applications.

An introduction to OpenID Connect in ASP.NET Core
An introduction to OpenID Connect in ASP.NET Core

Setting Up ASP.NET Core with Entity Framework Core

Before we delve into Code First, let's set up an ASP.NET Core project with Entity Framework Core.

ASP.NET Core + Entity Framework Core + jQuery to Delete Records without Page Reload
ASP.NET Core + Entity Framework Core + jQuery to Delete Records without Page Reload

First, create a new ASP.NET Core Web Application project using the command line or your favorite IDE. Then, add the Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.SqlServer packages to your project. These packages will enable Entity Framework Core and allow us to use SQL Server as our database.

Creating the Model Class

Introduction to Entity Framework Core - The Engineering Projects
Introduction to Entity Framework Core - The Engineering Projects

In Code First, we start by defining our model using plain old CLR objects (POCOs). Let's create a simple Blog model:

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
}

Creating the DbContext Class

Web Application, 10 Things
Web Application, 10 Things

Next, we'll create a DbContext class that will manage our database interactions:

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public DbSet<Blog> Blogs { get; set; }
}

Database Migration with Code First

Yardstick vs. .Net Core vs .NET Framework – Which One to Choose?
Yardstick vs. .Net Core vs .NET Framework – Which One to Choose?

Now that we have our model and DbContext, let's see how Code First handles the database.

By default, Code First uses a migration tool called ef migrations. This tool takes our current model and applies any differences to the database, ensuring it matches our code.

the asp net page lifecycle
the asp net page lifecycle
Change Tracking in ASP.NET Core | Entity Framework | C#
Change Tracking in ASP.NET Core | Entity Framework | C#
#dotnet #entityframework #webapi #linq #dotnetcore #interviewpreparation #careergrowth #csharp #backenddevelopment | Shaheen Aziz
#dotnet #entityframework #webapi #linq #dotnetcore #interviewpreparation #careergrowth #csharp #backenddevelopment | Shaheen Aziz
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
owasp top 10 web application vulnerabilities
owasp top 10 web application vulnerabilities
a poster with the words, using sol server & ef core
a poster with the words, using sol server & ef core
Compilemode    Online Tutorials IoT, Microsoft Azure, AWS, ASP.NET Core, C#,Amazon Web Service,SQL,CosmosDB, React, Angular
Compilemode Online Tutorials IoT, Microsoft Azure, AWS, ASP.NET Core, C#,Amazon Web Service,SQL,CosmosDB, React, Angular
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
Data - Anti join is a simple way to find what is missing.  In SQL, you can use LEFT JOIN with IS NULL to return rows from one table that have no matching row in another table.  Example:  Customers table → all customers Orders table → customers who ordered Anti join result → customers who have not placed any orders  The key pattern:  LEFT JOIN keeps all rows from the left table WHERE right_table.id IS NULL keeps only the unmatched rows  Useful for finding missing orders, unsold products, inactive users, or records that do not exist in another table.  Save this for your SQL problem-solving toolkit.  #SQL #SQLTips #AntiJoin #DataAnalysis #Database #DataCleaning #DataDrivenInsights | Facebook
Data - Anti join is a simple way to find what is missing. In SQL, you can use LEFT JOIN with IS NULL to return rows from one table that have no matching row in another table. Example: Customers table → all customers Orders table → customers who ordered Anti join result → customers who have not placed any orders The key pattern: LEFT JOIN keeps all rows from the left table WHERE right_table.id IS NULL keeps only the unmatched rows Useful for finding missing orders, unsold products, inactive users, or records that do not exist in another table. Save this for your SQL problem-solving toolkit. #SQL #SQLTips #AntiJoin #DataAnalysis #Database #DataCleaning #DataDrivenInsights | Facebook

Applying Migrations

To apply migrations, run the following commands in the Package Manager Console:

Add-Migration InitialCreate
Update-Database

These commands create a new migration and apply it to the database, creating the necessary schema to store our Blog model.

Adding Data to the Database

With our database set up, let's insert some data:

var blog = new Blog { Name = "ASP.NET Blog", Url = "http://asp.net" };
context.Blogs.Add(blog);
context.SaveChanges();

The DbSet.Add method inserts the new entity into the database, and DbContext.SaveChanges saves the changes to the database.

And that's a wrap! You've just seen how ASP.NET Core and Entity Framework Core work together using the Code First approach. Now, go forth and build amazing data-driven web applications!