Entity Framework Core Overview

Last updated: October 26, 2023

Entity Framework Core (EF Core) is a modern, open-source, cross-platform version of the popular Microsoft Entity Framework data access technology. It's a lightweight, extensible, and high-performance Object-Relational Mapper (ORM) for .NET.

Key takeaway: EF Core simplifies data access in .NET applications by allowing developers to work with a conceptual model instead of directly writing SQL queries.

What is Entity Framework Core?

EF Core enables developers to:

Key Features and Benefits

Performance

EF Core has been rewritten from the ground up with performance in mind. It includes optimizations for query execution, change tracking, and connection management, making it significantly faster than previous versions of Entity Framework.

Extensibility

EF Core is highly extensible. You can customize almost every aspect of its behavior, including:

Cross-Platform Support

EF Core runs on Windows, macOS, and Linux, allowing you to develop and deploy your applications on any platform.

Modern .NET Development

EF Core integrates seamlessly with modern .NET features and patterns, including:

EF Core Architecture

EF Core's architecture is based on a few core components:

Getting Started with EF Core

To start using EF Core, you'll typically follow these steps:

  1. Install EF Core NuGet packages: You'll need to install the appropriate EF Core packages for your chosen database provider. For example, to use SQL Server:
    dotnet add package Microsoft.EntityFrameworkCore.SqlServer
    And for migrations:
    dotnet add package Microsoft.EntityFrameworkCore.Design
  2. Define your model classes: Create POCO (Plain Old CLR Object) classes that represent your database entities.
  3. Create a DbContext: Create a class that derives from DbContext and defines DbSet<TEntity> properties for your entities.
  4. Configure your DbContext: Configure the database provider and connection string in your application's startup.
  5. Generate migrations (optional): Use the EF Core tools to create initial migrations and update your database schema.
  6. Query and save data: Use your DbContext instance to query data using LINQ and to save changes (add, update, delete).

Example: Simple Query

Here's a basic example of querying data using EF Core:

// Assuming 'dbContext' is an instance of your DbContext

            var products = await dbContext.Products
                .Where(p => p.Price > 50)
                .OrderBy(p => p.Name)
                .ToListAsync();

            foreach (var product in products)
            {
                Console.WriteLine($"- {product.Name} (${product.Price})");
            }