public void StartOwnTransactionWithinContext() { using (var ctx = new tect_CodeFirstLingContext()) { 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(); } } } }
public void StartOwnTransactionWithinContext() { using (var ctx = new tect_CodeFirstLingContext()) { using (var dbContextTransaction = ctx.Database.BeginTransaction()) { try { var lecturerList = ctx.lecturers.ToList <lecturer>(); foreach (var item in ctx.lecturers) { Console.WriteLine(item.lc_lname + " " + item.lc_fname); } Console.WriteLine("---------------------------------"); //Perform create operation Console.WriteLine("Perform create operation"); ctx.lecturers.Add(new lecturer() { lc_id = "L_8", lc_fname = "Second lecturer", lc_lname = "sdfs" }); //Execute Inser, Update & Delete queries in the database ctx.SaveChanges(); var lects1 = ctx.lecturers.ToList <lecturer>(); foreach (var item in ctx.lecturers) { Console.WriteLine(item.lc_lname + " " + item.lc_fname); } Console.WriteLine("---------------------------------"); //Perform Update operation Console.WriteLine("Perform Update operation"); var lecturerList1 = ctx.lecturers.ToList <lecturer>(); lecturer lecturerToUpdate = ctx.lecturers.Where(s => s.lc_fname == "Second lecturer").FirstOrDefault <lecturer>(); lecturerToUpdate.lc_fname = "Edited second"; //Execute Inser, Update & Delete queries in the database ctx.SaveChanges(); Console.WriteLine("Update is succesfully!"); dbContextTransaction.Commit(); foreach (var item in ctx.lecturers) { Console.WriteLine(item.lc_lname + " " + item.lc_fname); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } catch (System.Data.Entity.Infrastructure.DbUpdateException e) { Console.WriteLine(e.GetBaseException()); } } } }
static void Lect_email_ins_qry() { using (var ctx = new tect_CodeFirstLingContext()) { using (var dbContextTransaction = ctx.Database.BeginTransaction()) { try { var lecturerList = ctx.lecturers.ToList <lecturer>(); var lecturerItems = lecturerList.OrderByDescending(i => i.lc_id).Take <lecturer>(1); Console.WriteLine("Find lecturer max key "); Console.WriteLine("Max: {0}", lecturerItems.ElementAt(0).lc_id); Console.WriteLine("Insert new key"); string new_lc_id = Console.ReadLine().ToString(); Console.WriteLine("Insert new first name"); string new_lc_fname = Console.ReadLine().ToString(); Console.WriteLine("Insert new last name"); string new_lc_lname = Console.ReadLine().ToString(); Console.WriteLine("Insert new email"); string new_email = Console.ReadLine().ToString(); List <lecturer> chlect = ctx.lecturers.ToList(); lecturer lectToUpd = chlect.Where(i => i.lc_id == new_lc_id).FirstOrDefault <lecturer>(); if (lectToUpd == null) { List <email> chmail = ctx.emails.ToList(); email emailToUpdate = chmail.Where(s => s.lc_id == new_lc_id).FirstOrDefault <email>(); if (emailToUpdate == null) { ctx.lecturers.Add(new lecturer() { lc_id = new_lc_id, lc_fname = new_lc_fname, lc_lname = new_lc_lname }); ctx.emails.Add(new email { em_value = new_email, lc_id = new_lc_id }); Console.WriteLine("Are you sure ? Type Y "); if (Console.ReadKey().Key == ConsoleKey.Y) { ctx.SaveChanges(); dbContextTransaction.Commit(); } else { dbContextTransaction.Rollback(); } } else { Console.WriteLine("Foreign key problem, email for this lc_id exists"); dbContextTransaction.Rollback(); } } else { Console.WriteLine("Duplicate key problem, lc_id exists"); dbContextTransaction.Rollback(); } } catch (Exception ex) { Console.WriteLine(ex.Message); dbContextTransaction.Rollback(); } } } }