You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to develop an application.
You use the ADO.NET Entity Framework Designer to model entities. The application includes two ObjectContext instances named context1 and context2.
You need to persist the changes in both object contexts within a single transaction. Which code segment should you use?
A.
using (TransactionScope scope = new TransactionScope())
{
context1.SaveChanges();
context2.SaveChanges();
}
B.
using (TransactionScope scope = new TransactionScope())
{
context1.SaveChanges();
context2.SaveChanges();
scope.Complete();
}
C.
using (TransactionScope scope = new TransactionScope())
{
using (TransactionScope scope1 = new TransactionScope(TransactionScopeOption.RequireNew))
{
context1.SaveChanges();
scope1.Complete();
}
using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.RequireNew))
{
context2.SaveChanges();
scope2.Complete();
}
scope.Complete();
}
D.
using (TransactionScope scope = new TransactionScope())
{
using (TransactionScope scope1 = new TransactionScope(TransactionScopeOption.RequireNew))
{
context1.SaveChanges();
}
using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.RequireNew))
{
context2.SaveChanges();
}
}
Explanation:
TransactionScope.Complete Indicates that all operations within the scope are completed successfully.TransactionScope Class
(http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx)