在 Linq 中使用 ExecuteCommand to Sql with Transaction

Use ExecuteCommand in Linq to Sql with Transaction

我想按以下方式使用 ExecuteCommand() 更新表格:

using (var context = new FMDataContext())
{
    // how do I execute below two steps in a single transaction?
    context.ExecuteCommand("Update Table1 set X = 1 Where Y = 2");
    context.ExecuteCommand("Update Table2 set X = 3 Where Y = 4");
}

有答案here for this but it's for EF, I am using Linq To Sql

您需要围绕您的通话 TransactionScope

using (TransactionScope transaction = new TransactionScope())
{

    using (var context = new FMDataContext())
    {            
        context.ExecuteCommand("Update Table1 set X = 1 Where Y = 2");
        context.ExecuteCommand("Update Table2 set X = 3 Where Y = 4");
    }
    transaction.Complete();
}