2
votes

We are planning to develop a new application in asp.net web forms. We are using Entity Framework (with POCO classes approach).

We have Presentation layer, Business logic layer and Data access layer. My question is that if on a page we want to do some transactional work, means add some data to customer,add some other data that is not relevant to customer, send an email e.t.c then how can we do it in a single transaction.

Either we should start transaction on the web page's code behind? or pass these all data to some customer method in BLL and it will do all this in transactional manner? in both ways code will not be loosely coupled.

Can anybody let me know the best practice to do a transactional work when there are different entities involved in the operation?

1

1 Answers

2
votes

Description

SaveChanges uses Transaction inside but if you want to perform multiple SaveChanges calls you need the TransactionScope. If scope.Complete() gets not called (exception happend) the transaction get a rollback.

Sample

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
     // add something to your context
     myDbContext.Add(...);
     // save changes
     myDbContext.SaveChanges();
     // commit changes 
     scope.Complete(); 
}

More Information