Answer the question
In order to leave comments, you need to log in
entity framework. How to use transactions correctly?
Colleagues, welcome!
I have the following code in my app
public static bool DoLargeOperation()
{
bool res = true;
res = res && DoFirstSubOperation();
res = res && DoSecondSubOperation();
res = res && DoThirdSubOperation();
return res;
}
public static bool DoFirstSubOperation()
{
using (var context = new EntityFrameworkContext())
{
// Изменение данных.
context.SaveChanges();
}
}
using (var transaction = new TransactionScope())
{
res = res && DoFirstSubOperation();
res = res && DoSecondSubOperation();
res = res && DoThirdSubOperation();
}
public static bool DoLargeOperation()
{
bool res = true;
using (var context = new EntityFrameworkContext())
{
using (var transaction = context.Database.BeginTransaction())
{
res = res && DoFirstSubOperation(context);
res = res && DoSecondSubOperation(context);
res = res && DoThirdSubOperation(context);
if (res)
{
transaction.Commit();
}
else
{
transaction.Rollback();
}
}
}
return res;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question