使用ninject的依赖注入RoleStore

时间:2016-06-08 13:34:59

标签: c# entity-framework ninject owin identity

嘿我试图将IRoleStore绑定到Rolestore但我一直收到错误。我为应用程序用户使用相同的代码,这很顺利,但作为示例,我将在此处显示一个代码:

kernel.Bind<IRoleStore<ApplicationRole>>().To<RoleStore<ApplicationRole>>()
    .WithConstructorArgument("context", 
        context => kernel.Get<InBuildingNavigatorDbContext>());
kernel.Bind<RoleManager<ApplicationRole>>().ToSelf().InRequestScope();

kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>()
    .WithConstructorArgument("context", 
        context => kernel.Get<InBuildingNavigatorDbContext>());
kernel.Bind<UserManager<ApplicationUser>>().ToSelf().InRequestScope();

我收到错误:

  

类型&#39; Microsoft.AspNet.Identity.EntityFramework.RoleStore&lt; InBuildingNavigator.Data.Models.ApplicationRole&gt;&#39;不能用作类型参数&#39; TImplementation&#39;通用类型或方法&#39; IBindingToSyntax&lt; IRoleStore&lt; ApplicationRole&gt;&gt; .To&lt; TImplementation&gt;()&#39; Microsoft没有隐式引用转换.AspNet.Identity.EntityFramework.RoleStore&lt; InBuildingNavigator.Data.Models.ApplicationRole&gt;&#39; to&#39; Microsoft.AspNet.Identity.IRoleStore&lt; InBuildingNavigator.Data.Models.ApplicationRole&gt;&#39;。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

盲注,但你可以试试:

kernel.Bind<IRoleStore<ApplicationRole, string>>().To<RoleStore<ApplicationRole>>()
    .WithConstructorArgument("context", 
        context => kernel.Get<InBuildingNavigatorDbContext>());

根据RoleStore documentation RoleStore实现IRoleStore<TRole, string>,而不是IRoleStore<TRole>