// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Data.Entity; using System.Linq; namespace Tx.NonXADB.EF { public class StandardTransaction : DB { public override MyDbContext GetDbContext() { return new MyDbContext("MYSQL"); } public override void Test() { for(int i = 0; i < 10; i++) { using(MyDbContext ctx = GetDbContext()) { using (DbContextTransaction tx = ctx.Database.BeginTransaction()) { try { TblSrc osrc = ctx.TblSrc.First(row => row.Id == 1); osrc.Val = osrc.Val - 1; ctx.SaveChanges(); if(i % 2 != 0) throw new Problem(); TblTrg otrg = ctx.TblTrg.First(row => row.Id == 1); otrg.Val = otrg.Val + 1; ctx.SaveChanges(); tx.Commit(); } catch(Problem) { tx.Rollback(); Console.WriteLine("Problem"); } } } } } public static void Main(string[] args) { Demo(new StandardTransaction()); Console.ReadKey(); } } }