Skip to content

Instantly share code, notes, and snippets.

@SeriousM
Last active February 22, 2020 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SeriousM/e6b30db2b21e7e602655 to your computer and use it in GitHub Desktop.
Save SeriousM/e6b30db2b21e7e602655 to your computer and use it in GitHub Desktop.
using (var transaction = new System.Transactions.TransactionScope())
{
// DBC = Database Command
// create the database context
var database = new DatabaseContext();
// search for the user with id #1
// DBC: BEGIN TRANSACTION
// DBC: select * from [User] where Id = 1
var userA = database.Users.Find(1);
// DBC: select * from [User] where Id = 2
var userB = database.Users.Find(2);
userA.Name = "Admin";
// DBC: update User set Name = "Admin" where Id = 1
database.SaveChanges();
userB.Age = 28;
// DBC: update User set Age = 28 where Id = 2
database.SaveChanges();
// DBC: COMMIT TRANSACTION
transaction.Complete();
}
// DBC = Database Command
// create the database context
var database = new DatabaseContext();
// search for the user with id #1
// DBC: select * from [User] where Id = 1
var userA = database.Users.Find(1);
// DBC: select * from [User] where Id = 2
var userB = database.Users.Find(2);
userA.Name = "Admin";
userB.Age = 28;
// DBC: BEGIN TRANSACTION
// DBC: update User set Name = "Admin" where Id = 1
// DBC: update User set Age = 28 where Id = 2
// DBC: COMMIT TRANSACTION
database.SaveChanges();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment