Skip to content
Long Le edited this page Sep 20, 2017 · 3 revisions

Built in transaction handling in UnitOfWork LinqPad Example

var productRepository = new Repository<Product>(this);
var product2 = await productRepository.FindAsync(7);
product2.Dump();

// Begin transaction
unitOfWork.BeginTransaction();

try
{
    product2.ProductName = "Chai4";
    product2.ObjectState = ObjectState.Modified; // always set the ObjectState

    // <Do other transactions here>

    productRepository.Update(product2);

    var changes = await unitOfWork.SaveChangesAsync();
    changes.Dump("changes");

    // Commit Transaction
    unitOfWork.Commit();
}
catch
{
	// Rollback transaction
	unitOfWork.Rollback();
}

product2 = await productRepository.FindAsync(7);
product2.Dump();