I am trying to update a user.
AppUserManager appUserManager = HttpContext.GetOwinContext().GetUserManager<AppUserManager>();
AppUser member = await appUserManager.FindByIdAsync(User.Identity.GetUserId());
member.HasScheduledChanges = true;
IdentityResult identityResult = appUserManager.Update(member);
If the subsequent web API call fails, I need to discard any changes for the user. I know about transactions, for example:
using (var context = HttpContext.GetOwinContext().Get<EFDbContext>())
{
using (var dbContextTransaction = context.Database.BeginTransaction())
{
try
{
member.HasScheduledChanges = true;
IdentityResult identityResult = appUserManager.Update(member);
context.SaveChanges();
dbContextTransaction.Commit();
}
catch
{
}
}
}
But will operations with the AppUserManager inside the try block be transactional? Also, do they use the same instance of EFDbContext? In other words, I donβt know if the var context at the beginning of the second code example will be used when calling the appUserManager "Update" method in the try block.
In addition, AppUserManager is created as follows:
public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options, IOwinContext context)
{
EFDbContext db = context.Get<EFDbContext>();
AppUserManager manager = new AppUserManager(new UserStore<AppUser>(db));
return manager;
}
source
share