ユーザーを更新しようとしています。
AppUserManager appUserManager = HttpContext.GetOwinContext().GetUserManager<AppUserManager>();
AppUser member = await appUserManager.FindByIdAsync(User.Identity.GetUserId());
member.HasScheduledChanges = true;
IdentityResult identityResult = appUserManager.Update(member);
Web API への後続の呼び出しが失敗した場合、ユーザーへの変更をロールバックする必要があります。次のようなトランザクションについて知っています。
using (var context = HttpContext.GetOwinContext().Get<EFDbContext>())
{
using (var dbContextTransaction = context.Database.BeginTransaction())
{
try
{
// Changes
member.HasScheduledChanges = true;
// Would this be transactional?
IdentityResult identityResult = appUserManager.Update(member);
context.SaveChanges();
dbContextTransaction.Commit();
}
catch //(Exception ex)
{
// dbContextTransaction.Rollback(); no need to call this manually.
}
}
}
しかし、try ブロック内で AppUserManager を使用して実行される操作はトランザクションになりますか? また、EFDbContext の同じインスタンスを使用していますか? つまり、2 番目のコード例の先頭にある var コンテキストが、try ブロック内の appUserManager の「Update」メソッド呼び出しによって使用されるかどうかはわかりません。
また、AppUserManager は次のように作成されます。
public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options, IOwinContext context)
{
EFDbContext db = context.Get<EFDbContext>();
AppUserManager manager = new AppUserManager(new UserStore<AppUser>(db));
// etc.
return manager;
}