// 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.Linq; using System.Transactions; namespace Tx.NonXADB.EF { public class Smart : DB { public override MyDbContext GetDbContext() { return new MyDbContext("MYSQL"); } public override void Test() { for(int i = 0; i < 10; i++) { using(TransactionScope tx = new TransactionScope()) { using(MyDbContext ctx = GetDbContext()) { 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.Complete(); } catch(Problem) { Console.WriteLine("Problem"); } } } } } public static void Main(string[] args) { Demo(new Smart()); Console.ReadKey(); } } }