我在翻译中写了3个陈述:
如果第一个和第二个成功执行,第三个因某些错误而失败,那么我想回滚第三个语句,前两个应该被保存。
有可能吗?
答案 0 :(得分:0)
using (var context = new YourContext())
{
using (var dbContextTransaction = context.Database.BeginTransaction())
{
try
{
//first for insert
//second for update
dbContextTransaction.Commit();
}
catch (Exception)
{
dbContextTransaction.Rollback();
}
}
using (var dbContextTransaction1 = context.Database.BeginTransaction())
{
try
{
//third for delete
dbContextTransaction1.Commit();
}
catch (Exception)
{
dbContextTransaction1.Rollback();
}
}
}
}