标签:nsa cut use update occurs single get command blank
原文:http://www.entityframeworktutorial.net/entityframework6/transaction-in-entity-framework.aspx
EF默认情况下,在执行SaveChanges()时,在事务中进行插入,更新或删除操作。
EF为每个操作启动新事务,并在操作完成时完成事务。
执行另一个此类操作时,将启动一个新的事务。
EF 6引入了database.BeginTransaction和Database.UseTransaction来提供更多的事务控制。 现在,可以在单个事务中执行多个操作,如下所示:
xecute multiple operations in a single transaction as shown below: using (System.Data.Entity.DbContextTransaction dbTran = context.Database.BeginTransaction( )) { try { Student std1 = new Student() { StudentName = "newstudent" }; context.Students.Add(std1); context.Database.ExecuteSqlCommand( @"UPDATE Student SET StudentName = ‘Edited Student Name‘" + " WHERE StudentID =1" ); context.Students.Remove(std1); //saves all above operations within one transaction context.SaveChanges(); //commit transaction dbTran.Commit(); } catch (Exception ex) { //Rollback transaction if exception occurs dbTran.Rollback(); } }
database.UseTransaction允许DbContext使用在EF之外启动的事务。
标签:nsa cut use update occurs single get command blank
原文地址:http://www.cnblogs.com/talentzemin/p/7389659.html