Entity Framework Core (EF Core) is a powerful Object-Relational Mapper (ORM) from Microsoft, allowing developers to interact with databases using .NET objects. It eliminates the need to write most of the data-access code and is a preferred choice for building modern .NET applications.

In this article, we’ll explore EF Core from scratch: what it is, how it works, and how you can use it to build robust, scalable data access layers for your applications.


What is Entity Framework Core?

Entity Framework Core is an open-source, cross-platform ORM that enables developers to work with databases using .NET objects. EF Core simplifies the data access layer by abstracting raw SQL queries and giving you a more intuitive programming experience.

EF Core supports:

  • Code-First approach (you write classes, and EF generates the DB)
  • Database-First approach (you scaffold models from an existing DB)
  • LINQ-based queries
  • Tracking and change detection
  • Migrations for version control of schema

Setting Up Entity Framework Core

To start using EF Core in a .NET project, follow these steps:

1. Create a New .NET Project

dotnet new console -n EfCoreDemo
cd EfCoreDemo

2. Install EF Core Packages

For SQL Server:

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools

Creating the Data Model

Let’s create a simple User model and a DbContext class.

User.cs

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}
User model

AppDbContext.cs

using Microsoft.EntityFrameworkCore;

public class AppDbContext : DbContext
{
    public DbSet<User> Users { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlServer("Server=.;Database=EfCoreDemoDb;Trusted_Connection=True;");
}
Db context

Migrations and Database Creation

Migrations help keep your database schema in sync with your models.

1. Add the Initial Migration

dotnet ef migrations add InitialCreate

2. Apply the Migration to Create the Database

dotnet ef database update

Now EF Core will create the database and Users table.

Performing CRUD Operations

Here’s how you can do basic Create, Read, Update, and Delete operations using EF Core:

Main class setup

Create

using var context = new AppDbContext();
context.Users.Add(new User { Name = "Alice", Email = "alice@example.com" });
context.SaveChanges();
Create

Read

var users = context.Users.ToList();
foreach (var user in users)
{
    Console.WriteLine($"{user.Id} - {user.Name}");
}
Read

Update

var user = context.Users.First();
user.Name = "Updated Name";
context.SaveChanges();
Update

Delete

var user = context.Users.First();
context.Users.Remove(user);
context.SaveChanges();
Delete

Relationships and Navigation Properties

EF Core allows modeling relationships using navigation properties:

public class Blog
{
    public int Id { get; set; }
    public string Url { get; set; }
    public List<Post> Posts { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

EF will automatically create a one-to-many relationship between Blog and Post.

LINQ and Querying Data

EF Core supports LINQ for querying:

var blogWithPosts = context.Blogs
    .Include(b => b.Posts)
    .FirstOrDefault(b => b.Id == 1);

Use .AsNoTracking() for read-only operations to improve performance:

var users = context.Users.AsNoTracking().ToList();

GitHub Source Code

You can view the full source code for this tutorial on GitHub:

👉 https://github.com/bavindudissa/EF-Core.git


Best Practices

  • Use async methodsawait context.Users.ToListAsync()
  • Separate DbContext into a separate project for larger solutions
  • Use Dependency Injection (especially in ASP.NET Core projects)
  • Avoid querying unnecessary data (use Select and filtering)
  • Handle exceptions properly with try-catch blocks

Common Pitfalls

ProblemSolution
N+1 QueriesUse .Include() to eager load
Long MigrationsUse separate migration files, keep models clean
Connection string exposureUse secrets manager or environment variables

Conclusion

Entity Framework Core is a modern, lightweight, and powerful ORM that integrates seamlessly with .NET applications. Whether you’re building web APIs, desktop apps, or microservices, EF Core helps you stay productive and write clean, maintainable code.

Further Reading:

EF Core Docs

Awesome EF Core GitHub Resources