我正在使用EF 6 Code Firs
t并创建User
和A Role
,并将user
与Role
方法中的Seed
相关联。
AppUserManager userManager = new AppUserManager(new UserStore<AppUser>(context));
AppRoleManager roleManager = new AppRoleManager(new RoleStore<AppRole>(context));
string roleName = "Administrators";
string userName = "Admin";
string password = "Admin@2016";
string email = "admin@admin.com";
if (roleManager.RoleExists(roleName))
{
roleManager.Create(new AppRole(roleName));
}
AppUser user = userManager.FindByName(userName);
if (user == null)
{
userManager.Create(new AppUser {UserName = userName, Email = email}, password);
user = userManager.FindByName(userName);
}
if (!userManager.IsInRole(user.Id, roleName))
userManager.AddToRole(user.Id, roleName);
但我得到了conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated error while executing the following statement.
userManager.Create(new AppUser {UserName = userName, Email = email}, password);
IdentityUser
基类中有一个名为LockoutEndDateUtc
的字段,其类型为DateTime
?在数据库中,它被正确建模为nullable
。
我看过其他帖子,指示我填充日期字段。但是因为它是nullable
我为什么要填充它?
然而我试图像这样初始化它:
LockoutEndDateUtc = DateTime.UtcNow.AddYears(3)
但仍然没有运气。
我不明白为什么会出现这个错误。请帮忙。
由于