使用CodeFirst时DateTime转换错误

时间:2016-08-15 07:15:21

标签: entity-framework code-first

我正在使用EF 6 Code Firs t并创建User和A Role,并将userRole方法中的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) 但仍然没有运气。

我不明白为什么会出现这个错误。请帮忙。

由于

0 个答案:

没有答案