为什么我会得到一个错误IdentityRole? [英] Why am I getting an IdentityRole error?
问题描述
,在数据库中创建的用户,没有在账户控制器创建一个身份和登录用户code,这是我得到的错误。这里是code产生错误:
VAR身份=等待UserManager.CreateIdentityAsync(用户,DefaultAuthenticationTypes.ApplicationCookie);
这里是错误:
的实体类型IdentityRole不是模型对当前的一部分
上下文。
块引用>我的模型是这样的:
命名空间MyApp.Models
{
公共类ApplicationUser:IdentityUser
{
[需要]
[StringLength(50)]
公共字符串名字{获得;组; } [需要]
[StringLength(50)]
公共字符串名字{获得;组; }
} 公共类ApplicationRole:IdentityRole
{
[需要]
[StringLength(50)]
公共字符串ProperName {搞定;组; } [需要]
公共字符串描述{搞定;组; }
}
公共类MyAppDb:IdentityDbContext< ApplicationUser,ApplicationRole,串,IdentityUserLogin,IdentityUserRole,IdentityUserClaim>
{
公共MyAppDb()
:基地(MyAppDb)
{
}
}
}我的
的UserManager
是这样的账户控制器中创建:命名空间MyApp.Controllers
{
[授权]
公共类的AccountController:BaseController
{
私人的UserManager< ApplicationUser> _userManager; 公众的AccountController()
{
} 公众的AccountController(的UserManager< ApplicationUser>的UserManager)
{
的UserManager =的UserManager;
} 公众的UserManager< ApplicationUser>的UserManager
{
得到
{
返回_userManager =新的UserManager&所述; ApplicationUser>(新UserStore&所述; ApplicationUser>(新MyAppDb()));
}
私订
{
_userManager =价值;
}
} ...
}
}
解决方案我花了很多时间在这个问题上,并张贴许多问题要得到它的底部。我当然希望这个信息可以帮助其他人。
基本上,定制
用户
是容易的,有几个可用的例子。然而,自定义角色
是有问题的,我发现的唯一的例子是定制的,远远超过了来自开箱即用,我能解决一个后得到一个工作编号
问题。在这种情况下的问题是,我不得不创建编号
自己。下面是这个问题:<一href=\"http://stackoverflow.com/questions/22698744/usermanager-createuser-password-thowing-entityvalidationerror-saying-id-is-re\">UserManager.Create(user,密码)异常触发EntityValidationError说时需出示身份证?因此,在简单的例子,注意当你刚自定义
用户
从<$ C $你的的DbContext
继承C> IdentityDbContext ,您只需提供IdentityUser
,在我的情况下,它被称为ApplicationUser
看着这样的IdentityDbContext&LT; ApplicationUser&GT;
。还要注意,的UserManager
和UserStore
提供了IdentityUser
同时,在大多数例子看起来像这样新的UserManager&LT; ApplicationUser&GT;(新UserStore&LT; ApplicationUser&GT;(新ApplicationDbContext()))
但是,一旦你自定义您的
角色
你需要传递的一切,包括密钥类型TKEY的
的编号
字段。我的是这样IdentityDbContext&LT; ApplicationUser,ApplicationRole,串,IdentityUserLogin,IdentityUserRole,IdentityUserClaim&GT;
因为我只定制了用户
和角色
。但你还没有完成,因为正规
的UserManager
已经不明白。的我想这是身份的乡亲可以/应该解决,但我离题。的所以我必须实现我自己的UserStore
和的UserManager
和通知(这是我绊倒的部分)您必须提供KType
点击这里太或实例化自定义的的UserManager
会给你一个编译时错误:公共类ApplicationUserStore:UserStore&LT; ApplicationUser,ApplicationRole,串,IdentityUserLogin,IdentityUserRole,IdentityUserClaim&GT;
{
公共ApplicationUserStore(MyAppDb上下文)
:基座(上下文)
{
}
}公共类ApplicationUserManager:&的UserManager LT; ApplicationUser,串&GT;
{
公共ApplicationUserManager(IUserStore&LT; ApplicationUser,串&GT;存储)
:基地(存储)
{
}
}和实例化
的UserManager
在账户控制器这样的新ApplicationUserManager(新ApplicationUserStore(新MyAppDb()))
:公共类的AccountController:BaseController
{
私人ApplicationUserManager _userManager; 公众的AccountController()
{
} 公众的AccountController(ApplicationUserManager的UserManager)
{
的UserManager =的UserManager;
} 公共ApplicationUserManager的UserManager
{
得到
{
返回_userManager =新ApplicationUserManager(新ApplicationUserStore(新MyAppDb()));
}
私订
{
_userManager =价值;
}
} ...}希望这有助于!
Using Identity 2.0 After registering a user, the user is created in the database, there is code in the Account controller to create an identity and sign the user in. This is when I get the error. Here is the code producing the error:
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
And here is the error:
The entity type IdentityRole is not part of the model for the current context.
My Model looks like this:
namespace MyApp.Models { public class ApplicationUser : IdentityUser { [Required] [StringLength(50)] public string FirstName { get; set; } [Required] [StringLength(50)] public string LastName { get; set; } } public class ApplicationRole : IdentityRole { [Required] [StringLength(50)] public string ProperName { get; set; } [Required] public string Description { get; set; } } public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> { public MyAppDb() : base("MyAppDb") { } } }
My
UserManager
is created like this in the Account controller:namespace MyApp.Controllers { [Authorize] public class AccountController : BaseController { private UserManager<ApplicationUser> _userManager; public AccountController() { } public AccountController(UserManager<ApplicationUser> userManager) { UserManager = userManager; } public UserManager<ApplicationUser> UserManager { get { return _userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyAppDb())); } private set { _userManager = value; } } ... } }
解决方案I've spent a lot of time on this issue and posted many questions to get to the bottom of it. I sure hope this information helps other folks.
Basically, customizing the
User
is easy and there are several examples available. However, customizing theRole
is problematic and the only examples I found were customized far beyond what comes "out of the box" and I was able to get one working after resolving anId
issue. The issue in that case was that I had to create theId
myself. Here is that issue: UserManager.Create(user, password) thowing EntityValidationError saying Id is required?So in simple examples, notice when you are just customizing the
User
yourDbContext
inherits fromIdentityDbContext
and you just provide theIdentityUser
, in my case it was calledApplicationUser
and looked like thisIdentityDbContext<ApplicationUser>
. Also notice that theUserManager
andUserStore
is provided theIdentityUser
as well, and in most examples looks like thisnew UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))
But once you customize your
Role
you need to pass everything, including the key typeTKey
of theId
fields. Mine looks like thisIdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
because I only customized theUser
andRole
.But you aren't done yet, because the regular
UserManager
doesn't understand anymore. I think this is something the Identity folks could/should fix, but I digress. So I had to implement my ownUserStore
andUserManager
and notice (and this is the part that tripped me up) you have to provide theKType
here too or instantiating your customUserManager
will give you a compile-time error:public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> { public ApplicationUserStore(MyAppDb context) : base(context) { } } public class ApplicationUserManager : UserManager<ApplicationUser, string> { public ApplicationUserManager(IUserStore<ApplicationUser, string> store) : base(store) { } }
And instantiate the
UserManager
in the Account controller like thisnew ApplicationUserManager(new ApplicationUserStore(new MyAppDb()))
:public class AccountController : BaseController { private ApplicationUserManager _userManager; public AccountController() { } public AccountController(ApplicationUserManager userManager) { UserManager = userManager; } public ApplicationUserManager UserManager { get { return _userManager = new ApplicationUserManager(new ApplicationUserStore(new MyAppDb())); } private set { _userManager = value; } } ... }
Hope this helps!
这篇关于为什么我会得到一个错误IdentityRole?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!