using (var unitOfWork = unitOfWorkManager.Begin()) { // Perform some database operations context.SaveChanges(); unitOfWorkManager.Commit(); }
using (var unitOfWork = unitOfWorkManager.Begin()) { try { // Perform some database operations context.SaveChanges(); unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.Rollback(); throw; } }In this example, we are wrapping our database operations in a try-catch block and rolling back the transaction if an exception occurs by calling the Rollback() method. Package library: Microsoft.Extensions.DependencyInjection.EntityFramework.