I want to create a user with a role in the same transaction but i have an issue with the implementation. In order to use the userStore in the transaction and have it not save the changes automatically and ignore my transaction i had to turn off AutoSaveChanges. This makes it so it will wait until i call save changes. This works fine but because the userstore now does not return the userId when i call manager.Create due to this being off I dont have an Id to pass into userManager.AddToRole. Is there any way to add the user i am trying to create to a role within the same transaction?
Asked
Active
Viewed 1,871 times
1 Answers
3
If you start your transaction manually, then commit it, everything that was written to DB inside your transaction will be held inside your transaction. And you can rollback that if you want to.
Do something like that:
var dbContext = // get instance of your ApplicationDbContext
var userManager = // get instance of your ApplicationUserManager
using (var transaction = dbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted))
{
try
{
var user = // crate your ApplicationUser
var userCreateResult = await userManger.CreateAsync(user, password);
if(!userCreateResult.Succeeded)
{
// list of errors in userCreateResult.Errors
transaction.Rollback();
return userCreateResult.Errors;
}
// new Guid for user now saved to user.Id property
var userId = user.Id;
var addToRoleresult = await userManager.AddToRoleAsync(user.Id, "My Role Name");
if(!addToRoleresult.Succeeded)
{
// deal with errors
transaction.Rollback();
return addToRoleresult.Errors;
}
// if we got here, everything worked fine, commit transaction
transaction.Commit();
}
catch (Exception exception)
{
transaction.Rollback();
// log your exception
throw;
}
}
Hope this helps.

trailmax
- 34,305
- 22
- 140
- 234
-
What if dbContext is different to the context used to create an instance of ApplicationUserManager? Will this still work? – nmit026 Apr 14 '16 at 23:46
-
@nmit026 In theory, transaction is a database-level operation, so it should work with two dbContexts, but I have never tried. Possibly the transaction created above is a connection-level transaction; and two dbContexts will have two different connections, so it might not work on this basis. Perhaps the transaction should be started in a different way for your case: `using(TransactionScope tran = new TransactionScope()) { // do work; tran.Complete(); }` See more here: http://stackoverflow.com/a/224702/809357 – trailmax Apr 15 '16 at 09:18
-
@nmit026 bear in mind, Identity uses `async` all over the place, so your transaction scope needs to be created with that in mind: `var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)` – trailmax Apr 15 '16 at 09:23
-
Thank you! Perhaps you could look at this question I asked today: http://stackoverflow.com/questions/36636272/transactions-with-asp-net-identity-usermanager – nmit026 Apr 15 '16 at 10:30