ASP.net Identity Custom Tables using OwinContext

2019-08-18 06:39发布

I'm trying to implement custom tables to store users/roles etc. in a SQL server DB using Entity framework 6.

I've create a base DbContext that derives from IdentityDbContext

 public class MainContext : IdentityDbContext<ApplicationUser>
 {
    public MainContext()
        : base("name=Main")
    {
    }

    public static MainContext Create()
    {
        return new MainContext();
    }
  }

I also have a custom user class that inherits IdentityUser

public class ServiceUser : IdentityUser{

}

In the ConfigureAuth the defualt code was:

public void ConfigureAuth(IAppBuilder app)
        {                
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

 ..
}

I want to be able to use my MainContext here instead of the ApplicationDBContext, when I try the following

app.CreatePerOwinContext(MainContext.Create());

I get an error

'The type arguments for method 'Owin.AppBuilderExtensions.CreatePerOwinContext(Owin.IAppBuilder, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly. '

Considering the default ApplicationDbContext looks like:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("Main", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

I cannot see what is different that would cause this error?

2条回答
Explosion°爆炸
2楼-- · 2019-08-18 07:11

Foolish mistake, it should have been

app.CreatePerOwinContext(MainContext.Create);

and not

app.CreatePerOwinContext(MainContext.Create());
查看更多
看我几分像从前
3楼-- · 2019-08-18 07:22

Try with

app.CreatePerOwinContext<IdentityDbContext>(MainContext.Create);
查看更多
登录 后发表回答