AbbasFaisal
AbbasFaisal

Reputation: 1490

Use ExecuteCommand in Linq to Sql with Transaction

I want to update tables using ExecuteCommand() in below manner:

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");
}

There is an answer here for this but it's for EF, I am using Linq To Sql.

Upvotes: 1

Views: 797

Answers (1)

Mong Zhu
Mong Zhu

Reputation: 23732

you need to span a TransactionScope around your calls:

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();
}

Upvotes: 2

Related Questions