ASP.NET MVC 5 Entity Framework – Transaction Scope + SaveChange 方法
try { TestEntities entity = new TestEntities(); using (var dbContextTransaction = entity.Database.BeginTransaction()) { testing_tbl tbl_record; for (int i = 1; i < 11; i++) { tbl_record = new testing_tbl(); tbl_record.T_Value1 = "V1 " + i.ToString(); tbl_record.T_Value2 = "V2 " + i.ToString(); tbl_record.T_Value3 = "V3 " + i.ToString(); entity.testing_tbl.Add(tbl_record); entity.SaveChanges(); } dbContextTransaction.Commit(); } }catch(Exception ex){ Console.WriteLine(ex.Message); Console.ReadLine(); } |