Back to Curriculum

Entity Framework Core

📚 Lesson 5 of 15 ⏱️ 70 min

Entity Framework Core

70 min

Entity Framework Core is a lightweight, extensible, open-source, and cross-platform version of Entity Framework, serving as an object-relational mapping (ORM) framework for .NET. EF Core enables you to work with databases using .NET objects instead of writing raw SQL. It provides LINQ support, change tracking, migrations, and database provider abstraction. Understanding EF Core enables efficient database access. EF Core is the recommended data access technology for ASP.NET Core.

EF Core serves as an object-relational mapping (ORM) framework, translating between object-oriented code and relational databases. EF Core maps C# classes (entities) to database tables, properties to columns, and relationships to foreign keys. This abstraction enables database-agnostic code. Understanding ORM concepts enables effective EF Core usage. EF Core simplifies database operations significantly.

EF Core supports many database engines (SQL Server, PostgreSQL, MySQL, SQLite, Oracle, etc.) and can be used for both relational and NoSQL databases through providers. Database providers translate EF Core operations to database-specific SQL. This enables switching databases with minimal code changes. Understanding database providers enables flexible data storage. EF Core's provider model enables database portability.

EF Core DbContext represents a session with the database and is used to query and save data. DbContext manages entity tracking, change detection, and database connections. You define DbContext by creating a class that inherits from DbContext and includes DbSet properties for your entities. Understanding DbContext enables database operations. DbContext is central to EF Core usage.

EF Core migrations track and apply database schema changes, enabling version control for database structure. Migrations are code files that describe schema changes. They can be applied to update databases or rolled back to undo changes. Understanding migrations enables evolving database schemas safely. Migrations are essential for team collaboration and deployment.

Best practices include using async methods for database operations, using Include() for eager loading related data, understanding query execution (deferred vs immediate), using migrations for schema changes, and optimizing queries to avoid N+1 problems. Understanding EF Core enables efficient database access. EF Core is powerful but requires understanding its behavior for optimal performance.

Key Concepts

  • Entity Framework Core is an ORM framework for .NET.
  • EF Core maps C# classes to database tables.
  • EF Core supports multiple database engines through providers.
  • DbContext represents a session with the database.
  • Migrations track and apply database schema changes.

Learning Objectives

Master

  • Setting up Entity Framework Core with databases
  • Creating DbContext and entity models
  • Using LINQ to query data
  • Creating and applying migrations

Develop

  • Understanding ORM concepts and benefits
  • Designing efficient database access patterns
  • Appreciating EF Core's role in data access

Tips

  • Use async methods (ToListAsync, SaveChangesAsync) for database operations.
  • Use Include() to eager load related data and avoid N+1 queries.
  • Use migrations for all schema changes—don't modify database directly.
  • Understand when queries execute (deferred vs immediate).

Common Pitfalls

  • Not using async methods, blocking threads unnecessarily.
  • Creating N+1 queries by not using Include() for related data.
  • Not using migrations, causing schema drift between environments.
  • Not understanding query execution, causing performance issues.

Summary

  • Entity Framework Core is an ORM framework for .NET.
  • EF Core maps objects to database tables.
  • DbContext manages database sessions and operations.
  • Migrations enable version-controlled schema changes.
  • Understanding EF Core enables efficient database access.

Exercise

Set up Entity Framework Core with a simple Product entity.

// Product.cs
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

// ApplicationDbContext.cs
public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    
    public DbSet<Product> Products { get; set; }
}

// Program.cs
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

Code Editor

Output