What is Entity Framework Core, and how does it relate to the Repository and Unit of Work patterns?
Short answer
EF Core is an object-relational mapper (ORM) that lets you work with a relational database using C# objects (entities) and LINQ queries instead of writing raw SQL by hand, translating LINQ expressions into SQL and mapping query results back into strongly-typed objects.
Advertisement
AModel answer
EF Core is an object-relational mapper (ORM) that lets you work with a relational database using C# objects (entities) and LINQ queries instead of writing raw SQL by hand, translating LINQ expressions into SQL and mapping query results back into strongly-typed objects. A DbContext itself already implements much of the Unit of Work pattern (tracking changes, committing them together via SaveChanges), and a DbSet<T> serves as a Repository-like abstraction over a table, which is why many teams skip additional Repository/UoW layers on top of EF Core as often redundant.
public class AppDbContext : DbContext {
public DbSet<Product> Products { get; set; }
}
var product = await context.Products.FindAsync(1);
product.Price = 29.99m;
await context.SaveChangesAsync(); // Unit of Work: commits all tracked changes togetherReal-world example
A team initially wraps EF Core in a custom Repository/UnitOfWork layer for testability, then later removes it after realizing DbContext already provides equivalent abstraction and the extra layer just added indirection without meaningful benefit.
Likely follow-ups
- When would an additional Repository layer over EF Core still be justified?
- How does DbContext's change tracking actually implement Unit of Work internally?
Related topics