此代码用于在数据库中创建角色。
IdentityConfig:
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(RoleStore<IdentityRole> roleStore)
: base(roleStore)
{
}
public static ApplicationRoleManager Create(IOwinContext context)
{
var roleStore = new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>());
return new ApplicationRoleManager(roleStore);
}
}
但在Startup.Auth中显示错误:
如何解决此错误?
修改
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
答案 0 :(得分:1)
CreatePerOwingContext<T>
的签名如下所示:
public static IAppBuilder CreatePerOwinContext<T>(this IAppBuilder app, Func<IdentityFactoryOptions<T>, IOwinContext, T> createCallback) where T : class, IDisposable
或者像这样:
public static IAppBuilder CreatePerOwinContext<T>(this IAppBuilder app, Func<T> createCallback) where T : class, IDisposable
即。你以太需要提供一个能够创建一个对象的函数,即
app.CreatePerOwinContext<ApplicationRoleManager>(() => new ApplicationRoleManager(/*blah params*/));
这将使用第二次覆盖。或者回到默认值:
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
var applicationDbContext = context.Get<ApplicationDbContext>();
var roleStore = new RoleStore<IdentityRole>(applicationDbContext);
var manager = new ApplicationRoleManager(roleStore);
return manager;
}