ASP.net Identity Custom Tables using OwinContext

柔情痞子 提交于 2019-12-13 05:09:59

问题


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?


回答1:


Try with

app.CreatePerOwinContext<IdentityDbContext>(MainContext.Create);



回答2:


Foolish mistake, it should have been

app.CreatePerOwinContext(MainContext.Create);

and not

app.CreatePerOwinContext(MainContext.Create());


来源:https://stackoverflow.com/questions/27781885/asp-net-identity-custom-tables-using-owincontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!