Featured Article

Entity Framework Core Tutorial Code First: Build Your Database from Scratch

Kenneth Jul 13, 2026

Discovering the power of Entity Framework Core (EF Core) through a Code First approach unlocks a rich, dynamic, and efficient way to interact with your databases. This hands-on tutorial will guide you through setting up an EF Core project, creating models, and working with databases using the celebrated Code First methodology. Let's dive in!

Free Entity Framework Book
Free Entity Framework Book

EF Core, a modern, extensible, and cross-platform version of Entity Framework, is designed to work with various database systems such as SQL Server, PostgreSQL, MySQL, and SQLite, just to name a few. It simplifies database interactions, allowing developers to focus on their core business logic. But what makes EF Core a game-changer is its support for Code First, enabling you to define your schema and models through C# classes rather than SQL scripts.

Entity Framework Core Code-First Tutorial
Entity Framework Core Code-First Tutorial

Setting Up Your Environment

Before we start, ensure you have .Net Core 3.1 or later installed on your machine. Install the Entity Framework Core tools globally via NuGet packet manager using the command: dotnet tool install --global dotnet-ef

Entity Framework Core Database-First Tutorial for .NET Core
Entity Framework Core Database-First Tutorial for .NET Core

Next, create a new .Net Core console application: dotnet new console -n EfCoreTutorial Replace 'EfCoreTutorial' with the desired name for your project.

Creating the Model

Entity Framework Core Tutorials
Entity Framework Core Tutorials

Create a new directory named 'Models' in your project and add a new C# class named 'Blog'. This will be our basic model representing a simple blog with an id, url, and name.

 namespace EfCoreTutorial.Models
{
    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
    }
}

Creating the DbContext Class

In the root directory of your project, create a new file named 'ApplicationDbContext.cs'. This class will interact with the database, manage our entities, and execute database operations.

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

 using Microsoft.EntityFrameworkCore;
 
namespace EfCoreTutorial
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        { }
 
        public DbSet<Blog> Blogs { get; set; }
    }
}

Configuring the Database

In the 'Program.cs' file, configure the connection string and add-migration command for creating your database schema.

Connect to a database using the 'UseSqlServer' method, specifying your connection string. Then, create an instance of the 'ApplicationDbContext' class and call the 'Add-Migration' command to generate SQL scripts based on the 'Blog' model. Ensure you have the correct connection string based on your chosen database system.

Entity Framework Core and calling a stored procedure
Entity Framework Core and calling a stored procedure

Running Migrations

Navigate to the terminal, run dotnet ef migrations script to update your database schema based on the generated SQL scripts.

Entity Framework Core In 60 Minutes : Entity Framework Core Tutorial
Entity Framework Core In 60 Minutes : Entity Framework Core Tutorial
Data Access in ASP.NET Core using EF Core (Code First)
Data Access in ASP.NET Core using EF Core (Code First)
Core First Approach of Entity Framework in ASP.NET Core MVC | Class 11
Core First Approach of Entity Framework in ASP.NET Core MVC | Class 11
Create an ASP .NET Core Site with Entity Framework - Kill All Defects
Create an ASP .NET Core Site with Entity Framework - Kill All Defects
👨‍🏫 C# (WinForms) SQL Server: Entity Framework Core (EF Core 5.0) Tutorial - Database First.
👨‍🏫 C# (WinForms) SQL Server: Entity Framework Core (EF Core 5.0) Tutorial - Database First.
a computer screen shot of the index page for an internet site that has been updated
a computer screen shot of the index page for an internet site that has been updated
2. Entity Framework and MVC Tutorial  2  Step by Step for Absolute Beginners
2. Entity Framework and MVC Tutorial 2 Step by Step for Absolute Beginners
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
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

Now that your database is set up, we can start performing CRUD operations using the Code First approach.

Performing CRUD Operations

Add methods to the 'Program.cs' file for creating, reading, updating, and deleting blog entries. Use LINQ queries to interact with the database through the 'ApplicationDbContext' instance.

Experience the power of Code First with Entity Framework Core and boost your productivity while writing clean, maintainable, and efficient code. Happy coding!

As you've seen, Entity Framework Core significantly simplifies working with databases using its Code First methodology. With its rich feature set and cross-database support, EF Core is a valuable tool in any developer's toolbox. Now that you're comfortable with the basics, explore more advanced topics, such as migrations, concatenating queries, or even integrating EF Core with your favorite API framework. Keep learning, and until next time, happy coding!