Transactions / Database.BeginTransaction and IdentityManager

I have a complicated add process to create a user (and add roles + other user related information to other tables)

Does anyone know how to use transactions when adding user / role or other Identity objects. I can not access "Database.BeginTransaction"

I have the following UserManager class, I'm not sure how I can access the "store" base class

public class UserManager : UserManager<ApplicationUser>
{
    public UserManager()
    : base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
    {
        //allows alphanumeric names in username
        UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
    }

}

There seems to be no way to access UserManager.UserStore.Database ...

thanks

+4
source share
1 answer

ASP.NET Identity Framework EntityFramework. DbContext - EF, TransactionScope .

EF Code First DBContext and Transactions

using(var scope = new TransactionScope(TransactionScopeOption.Required,
    new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
{
    // Do something like Add a User 
    context.SaveChanges();
    // Do something like Add a User to Role
    context.SaveChanges();

    scope.Complete();
}

1: - DbContext.Database IdentityManager.Store.Context.Database - DbContext, ConnectionString ( SqlConnection)

, BeginTransaction. → - Entity Framework 6 DbContext.Database.BeginTransaction?

2:

EF6 BeginTransaction: (EF6 Onwards)

ApplicationDbContext ctx = new ApplicationDbContext();
using (DbContextTransaction tran1 = ctx.Database.BeginTransaction())
{
    using (MyDbContext ctx2 = new MyDbContext(ctx.Database.Connection, false))
    {
        ctx2.Database.UseTransaction(tran1.UnderlyingTransaction);
    }
}

(EF6 Onwards)

. contextOwnsConnection false. , Entity Framework, , .

+4

All Articles