public void StartOwnTransactionWithinContext() { using (var ctx = new tect_CodeFirstContext()) { using (var dbContextTransaction = ctx.Database.BeginTransaction()) { try { var lecturerList = ctx.lecturers.ToList <lecturer>(); //Perform create operation Console.WriteLine("Perform create operation"); ctx.lecturers.Add(new lecturer() { lc_id = "L_2", lc_fname = "Second lecturer" }); //Execute Inser, Update & Delete queries in the database ctx.SaveChanges(); var lects1 = ctx.lecturers.ToList <lecturer>(); //Perform Update operation Console.WriteLine("Perform Update operation"); var lecturerList1 = ctx.lecturers.ToList <lecturer>(); lecturer lecturerToUpdate = lecturerList1.Where(s => s.lc_fname == "Second lecturer").FirstOrDefault <lecturer>(); lecturerToUpdate.lc_fname = "Edited second lecturer"; //Execute Inser, Update & Delete queries in the database ctx.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception) { dbContextTransaction.Rollback(); } } } }
static void Main(string[] args) { using (var ctx = new tect_CodeFirstContext()) { try { try { ctx.lecturers.Remove(ctx.lecturers.ToList <lecturer>().ElementAt(0)); ctx.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } var lecturerList = ctx.lecturers.ToList <lecturer>(); //Perform create operation Console.WriteLine("Perform create operation"); ctx.lecturers.Add(new lecturer() { lc_id = "L_1", lc_fname = "New lecturer" }); //Execute Insert, Update & Delete queries in the database ctx.SaveChanges(); var lects1 = ctx.lecturers.ToList <lecturer>(); Lecturers_print(lects1); //Perform Update operation Console.WriteLine("Perform Update operation"); var lecturerList1 = ctx.lecturers.ToList <lecturer>(); lecturer lecturerToUpdate = lecturerList1.Where(s => s.lc_fname == "New lecturer").FirstOrDefault <lecturer>(); lecturerToUpdate.lc_fname = "Edited lecturer"; //Execute Insert, Update & Delete queries in the database ctx.SaveChanges(); lects1 = ctx.lecturers.ToList <lecturer>(); Lecturers_print(lects1); Console.WriteLine(); SQL_qry(ctx); Console.WriteLine(); Console.WriteLine("Transaction example"); Transaction_test tr = new Transaction_test(); tr.StartOwnTransactionWithinContext(); lects1 = ctx.lecturers.ToList <lecturer>(); Lecturers_print(lects1); //Perform delete operation, re-read lecturers list Console.WriteLine("Perform delete operation"); lecturerList1 = ctx.lecturers.ToList <lecturer>(); ctx.lecturers.Remove(lecturerList1.ElementAt <lecturer>(0)); ctx.lecturers.Remove(lecturerList1.Where(s => s.lc_fname == "Edited second lecturer").FirstOrDefault <lecturer>()); //Execute Insert, Update & Delete queries in the database ctx.SaveChanges(); lects1 = ctx.lecturers.ToList <lecturer>(); Lecturers_print(lects1); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); } }