using System.Data.Entity; using MyProject.Models; public class MyDbContext : DbContext { public DbSetCustomers { get; set; } public DbSet Orders { get; set; } public virtual void Save() { var unitOfWork = new UnitOfWork (this); unitOfWork.AddAction(() => Customers.Add(new Customer())); unitOfWork.AddAction(() => Orders.Add(new Order())); unitOfWork.Commit(); } }
using System.Data.Entity; using MyProject.Models; public class MyDbContext : DbContext { public DbSetIn this example, we use the UnitOfWork pattern to delete a product from the Products table. We first search for the product to be deleted using the Find() method, and then add a new action to the UnitOfWork object that removes the product from the table. Finally, we call the Commit() method to execute the operation within a transaction. Package Library: The AddAction method is part of the Entity Framework package library. Specifically, it is a method of the UnitOfWork class that is part of the EntityFramework.dll assembly. This assembly can be installed using the NuGet package manager.Products { get; set; } public virtual void DeleteProduct(int productId) { var unitOfWork = new UnitOfWork (this); var product = Products.Find(productId); unitOfWork.AddAction(() => Products.Remove(product)); unitOfWork.Commit(); } }