LINQ to SQL: Transactions verwenden

Das Verwenden von Transactions in LINQ-to-SQL ist total simple:

using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())  {
    SomeDatabaseDataContext ctx = new SomeDatabaseDataContext();

    ctx.SomeEntities.InsertOnSubmit(new SomeEntity());
    ctx.SubmitChanges();

    ts.Complete();
}

Wichtig ist,das man den Aufruf von ts.Complete() nicht vergisst. Sonst wird ein Rollback auf die Transaktion gemacht!

Für den TransactionScope braucht man eine Referenz auf die System.Transactions.dll.

blog comments powered by Disqus