Entity Framework

Entity Framework (EF) is a modern object-relational mapper (ORM) for .NET that enables developers to work with databases using domain-specific objects, eliminating the need for most of the data-access code they'll typically need to write. EF Core is the latest version of Entity Framework, and it's the recommended ORM for new applications.

Note: This section primarily covers Entity Framework (EF) in general, with a focus on EF Core as the modern standard. For details on the older EF6, please refer to its specific documentation.

What is Entity Framework?

Entity Framework allows you to query a conceptual model, then translate these queries into a tabular data model that developers work with. It abstracts away much of the complexity of relational databases, allowing you to focus on your application logic. Key benefits include:

EF Core vs. EF6

Entity Framework has evolved significantly over the years. EF Core is a complete rewrite and redesign of EF, focusing on performance, extensibility, and cross-platform support. While EF6 is still supported, EF Core is the future of Entity Framework for .NET developers.

Tip: For new projects, it is highly recommended to use EF Core. It offers better performance, a more modular design, and is actively developed.

Key Concepts

1. The EF Core Model

The EF Core model is a representation of your data and how it maps to your database. This includes:

2. Database Providers

EF Core supports multiple database systems through various providers. Common providers include:

3. Migrations

Migrations are a feature of EF that allows you to incrementally update your database schema to match your EF model. This is crucial for evolving your database schema alongside your application.

Getting Started with EF Core

To begin using EF Core, you'll typically:

  1. Install the necessary EF Core NuGet packages for your project and database provider.
  2. Define your entity classes.
  3. Create a DbContext class.
  4. Configure your model and database connection.
  5. Use the DbContext to query and save data.

// Example: Installing EF Core for SQL Server
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
            

Example DbContext


using Microsoft.EntityFrameworkCore;

public class MyDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }

    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Further model configuration can go here
        modelBuilder.Entity<Product>().HasKey(p => p.ProductId);
        modelBuilder.Entity<Category>().HasKey(c => c.CategoryId);
    }
}

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public ICollection<Product> Products { get; set; }
}
            

Further Reading