Entity Framework

Entity Framework (EF) is a set of data access technologies developed by Microsoft that enables developers to work with relational data (like SQL Server, Oracle, MySQL, PostgreSQL, etc.) using domain-specific objects that are often referred to as entities. It eliminates the need for most of the data-access code that developers traditionally need to write.

Key Concepts

1. Code-First, Database-First, Model-First

Entity Framework provides different approaches for defining your data model:

  • Code-First: You define your entity classes in C# or Visual Basic, and EF generates the database schema based on your classes.
  • Database-First: You start with an existing database, and EF generates the entity classes and the DbContext from the database schema.
  • Model-First: You design your data model visually using a graphical designer, and EF generates the database schema and entity classes from the model.

2. DbContext

The DbContext class is the primary gateway to interacting with the EF data store. It represents a session with the database and allows you to query and save data. It typically maps to a specific database.


using Microsoft.EntityFrameworkCore;

public class MyDbContext : DbContext
{
    public DbSet Products { get; set; }
    public DbSet Categories { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;");
    }
}

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 Products { get; set; }
}
                

3. DbSet

A DbSet<TEntity> property on the DbContext represents a collection of all entities in the store for a given entity type. In simple terms, it's like a table in your database that EF knows how to interact with.

4. Migrations

Entity Framework Migrations is a feature that allows you to incrementally update your database schema as your domain model evolves. It enables you to manage changes to your database schema in a version-controlled manner.

Common commands:

  • Add-Migration InitialCreate: Creates a new migration.
  • Update-Database: Applies pending migrations to the database.

Benefits of Using Entity Framework

  • Productivity: Reduces boilerplate data access code significantly.
  • Maintainability: Centralizes data access logic.
  • Abstraction: Hides the complexities of SQL and provider-specific syntax.
  • Type Safety: Queries are strongly typed, catching errors at compile time.
  • Performance: Offers features like lazy loading, eager loading, and explicit loading to optimize data retrieval.

Versions

Entity Framework has evolved over time. Key versions include:

  • Entity Framework 6 (EF6): The last version of the "classic" EF, still supported.
  • Entity Framework Core (EF Core): The next generation of EF, cross-platform, open-source, and highly performant. It's the recommended choice for new development.

Getting Started

To start using EF Core, you'll typically need to install the appropriate NuGet packages:


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

Refer to the official tutorials for detailed walkthroughs and examples.