Entity Framework Explained
Entity Framework (EF) is an Object-Relational Mapping (ORM) framework that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. EF supports multiple database providers and offers various approaches to data access.
1. Key Concepts
Understanding the following key concepts is essential for mastering Entity Framework:
- DbContext: Represents a session with the database and allows you to query and save data.
- DbSet: Represents a collection of entities in the context.
- Model: The domain-specific objects and their relationships, defined using classes and attributes.
- Migrations: A way to keep the database schema in sync with the application's data model.
- LINQ to Entities: A query language used to interact with the database using EF.
- Lazy Loading: A technique where related data is loaded automatically when accessed.
- Eager Loading: A technique where related data is loaded along with the main query.
2. DbContext
The DbContext
class is the main class that coordinates Entity Framework functionality for a given data model. It acts as a bridge between your domain or entity classes and the database.
Example
public class MyDbContext : DbContext { public DbSet<Customer> Customers { get; set; } public DbSet<Order> Orders { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("YourConnectionStringHere"); } }
3. DbSet
The DbSet
class represents a collection of entities in the context. It provides methods for querying, adding, updating, and deleting entities.
Example
public class MyDbContext : DbContext { public DbSet<Customer> Customers { get; set; } } using (var context = new MyDbContext()) { var customers = context.Customers.ToList(); }
4. Model
The model is the domain-specific objects and their relationships, defined using classes and attributes. EF uses this model to generate the database schema.
Example
public class Customer { public int CustomerId { get; set; } public string Name { get; set; } public List<Order> Orders { get; set; } } public class Order { public int OrderId { get; set; } public DateTime OrderDate { get; set; } public int CustomerId { get; set; } public Customer Customer { get; set; } }
5. Migrations
Migrations allow you to evolve the database schema over time. They help keep the database schema in sync with the application's data model.
Example
// Add a new migration dotnet ef migrations add AddCustomerAddress // Update the database dotnet ef database update
6. LINQ to Entities
LINQ to Entities is a query language used to interact with the database using EF. It allows you to write strongly-typed queries against the database.
Example
using (var context = new MyDbContext()) { var customers = from c in context.Customers where c.Name.StartsWith("A") select c; }
7. Lazy Loading
Lazy Loading is a technique where related data is loaded automatically when accessed. It is useful for performance optimization but can lead to the N+1 query problem.
Example
using (var context = new MyDbContext()) { var customer = context.Customers.First(); var orders = customer.Orders; // Orders are loaded lazily }
8. Eager Loading
Eager Loading is a technique where related data is loaded along with the main query. It helps avoid the N+1 query problem by loading all necessary data in a single query.
Example
using (var context = new MyDbContext()) { var customers = context.Customers .Include(c => c.Orders) .ToList(); }