Use the Unit of Work pattern when a C#/.NET application needs one clear transaction boundary across multiple repository operations.
A Unit of Work owns or coordinates one EF Core DbContext. Repositories use that same context to stage changes. The application service or controller calls SaveChangesAsync once at the end of the logical operation.
AppDbContext
├─ DbSet<Customer>
├─ DbSet<Order>
└─ DbSet<Product>
IGenericRepository<TEntity>
GenericRepository<TEntity>
IUnitOfWork
├─ Customers
├─ Orders
├─ Products
└─ SaveChangesAsync()
DbContext, repositories, and Unit of Work with compatible lifetimes, usually scoped in ASP.NET Core.AsNoTracking for read-only queries.Include expressions over string include paths.DbContext access and Unit of Work access in the same workflow.Do not add a custom Unit of Work just for ceremony. EF Core DbContext already provides Unit of Work behavior. A custom abstraction is most useful when the project has a repository convention, a testing boundary, or complex operations spanning multiple repositories.
Powered by TurnKey Linux.