Featured Article

Net Core Entity Framework Code First Example Tutorial

Kenneth Jul 13, 2026

In the realm of modern .NET development, .NET Core and Entity Framework (EF) have become integral components, enabling developers to create high-performance, cross-platform applications with robust data access. One popular approach to get started with EF is the Code First methodology, which allows you to create your database schema through your code. Let's dive into an example of how to use .NET Core with Entity Framework Code First.

Free Entity Framework Book
Free Entity Framework Book

Before we begin, ensure you have installed the necessary packages: Microsoft.AspNetCorequête blond, Microsoft.EntityFrameworkCore, and Microsoft.EntityFrameworkCore.Tools. You can add them to your .NET Core project using the NuGet package manager in Visual Studio or the CLI with the `dotnet add package` command.

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

Theidity of Code First

The primary appeal of Code First lies in its inverted approach. Instead of creating your database first and then generating models from it (Database First), you define your data model using C# classes, and EF generates the database schema based on those definitions. This approach promotes a more natural, object-oriented workflow and encourages test-driven development.

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

Code First also supports migrations, which enable you to evolve your database schema over time. EF tracks changes in your models and applies them to your database, ensuring your data remains consistent and up-to-date.

Creating the Model Class

.Net Framework
.Net Framework

Let's start by defining a simple model class, `Blog`, to represent a blog post in our application.

```csharp public class Blog { public int Id { get; set; } public string Url { get; set; } public List Posts { get; set; } = new(); } ```

Configuring the DbContext

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

The `DbContext` class is the core of Entity Framework, responsible for interacting with the database. In this Code First example, we configure the DbContext to use our `Blog` model.

```csharp public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } public DbSet Blogs { get; set; } } ```

Migrations with Code First

Free .NET Framework Book
Free .NET Framework Book

Now that we have our model and DbContext configured, we can generate a migration to create the corresponding database schema.

To create a new migration, open your terminal or command prompt, navigate to your project directory, and run the following command:

A Complete Guide to Microsoft .NET Framework
A Complete Guide to Microsoft .NET Framework
Change Tracking in ASP.NET Core | Entity Framework | C#
Change Tracking in ASP.NET Core | Entity Framework | C#
an image of a computer screen with text
an image of a computer screen with text
.NET Core vs .NET Framework: What CTOs Must Know in 2026
.NET Core vs .NET Framework: What CTOs Must Know in 2026
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
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
Code photo
Code photo
How to Create a Self Contained Dotnet Application or a Framework Independent App Using .NET SDK CLI
How to Create a Self Contained Dotnet Application or a Framework Independent App Using .NET SDK CLI

```bash dotnet ef migrations add InitialCreate ```

This command generates a new migration class based on the changes detected in your model. The name "InitialCreate" is just an example; you can replace it with a more descriptive name representing this migration.

Applying the Migration

After creating the migration, you can apply it to your database using the following command:

```bash dotnet ef database update ```

This command updates your database schema based on the changes defined in the migration. It's essential to be cautious when running this command, as it may modify or even delete data in your database.

Seeding the Database

To add initial data to your database, you can create a seed method in your `DbContext`. This method will be called automatically when you create a new instance of your `DbContext` with the `EnsureCreated` boolean parameter set to `true`.

```csharp protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasData( new Blog {Id = 1, Url = "https://example.com"} ); } ```

This example seeds a single `Blog` entity into the database. You can use this approach to seed any number of entities or even complex relationships between them.

In conclusion, .NET Core and Entity Framework Code First empower you to create dynamic, efficient data-driven applications. By defining your data model through C# classes, you can easily evolve your database schema alongside your application. Embrace Code First and streamline your development process today!