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.
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:
- Productivity: Reduces boilerplate data access code.
- Maintainability: Code is cleaner and easier to manage.
- Abstraction: Works with various database providers.
- Type Safety: Leverages C# or VB.NET type system for database interactions.
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.
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:
- Entities: Plain Old CLR Objects (POCOs) that represent tables in your database.
- DbSet: Represents a collection of entities for a given type in a context.
- DbContext: The primary class that interfaces with the database. It represents a session with the database and can be used to query and save data.
- Relationships: How entities relate to each other (one-to-one, one-to-many, many-to-many).
2. Database Providers
EF Core supports multiple database systems through various providers. Common providers include:
- SQL Server (
Microsoft.EntityFrameworkCore.SqlServer
) - SQLite (
Microsoft.EntityFrameworkCore.Sqlite
) - PostgreSQL (
Npgsql.EntityFrameworkCore.PostgreSQL
) - MySQL (
Pomelo.EntityFrameworkCore.MySql
) - Cosmos DB (
Microsoft.EntityFrameworkCore.Cosmos
)
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.
Related API References
Getting Started with EF Core
To begin using EF Core, you'll typically:
- Install the necessary EF Core NuGet packages for your project and database provider.
- Define your entity classes.
- Create a
DbContext
class. - Configure your model and database connection.
- 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; }
}