I'm getting this strange error while creating IdentityRole
.
In my database initializer class i have
public class DatePickerDbInitializer:CreateDatabaseIfNotExists<DatePickerDbContext>
{
protected override void Seed(DatePickerDbContext context)
{
InitializeDatePickerDbForEf(context);
base.Seed(context);
}
private static void InitializeDatePickerDbForEf(DatePickerDbContext context)
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var licenseTrial = new License
{
Name = "Trial",
Description = "Works for 14 days"
};
var licenseFull = new License
{
Name = "Full",
Description = "Subscription based"
};
context.Licenses.Add(licenseTrial);
context.Licenses.Add(licenseFull);
var connectionDefault = new Connection
{
Name = "Default"
};
var conncetionSuperOffice = new Connection
{
Name = "SuperOffice"
};
context.Connections.Add(connectionDefault);
context.Connections.Add(conncetionSuperOffice);
roleManager.Create(new IdentityRole("Admin"));
roleManager.Create(new IdentityRole("NonAdmin"));
var user = new ApplicationUser()
{
UserName = "biplov",
Email = "foreverpunkrock@yahoo.com",
DateTimeRegistered = DateTime.UtcNow,
IsApproved = true,
LicenseId = licenseFull.Id,
};
var licenseInfo = new UserLicenseInfo
{
LicenseId = licenseFull.Id,
OwnerId = user.Id,
StartDateTime = DateTime.UtcNow
};
context.LicenseInfos.Add(licenseInfo);
var userConnection = new UserConnection()
{
UserId = user.Id,
ConnectionId = connectionDefault.Id
};
context.UserConnections.Add(userConnection);
userManager.AddToRoleAsync(user.Id, "Admin");
}
}
When I run program for the first time the InitializeDatePickerDbForEf method gets called.
And during execution of following line of code roleManager.Create(new IdentityRole("Admin"));
I get error which says: {"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated."}
How can I get error of datetime conversion when creating a role? Is the error being influenced by previous lines of code? (guess not)
Visual Studio Version: 2013 Asp.Net MVC version: 5.1 Database: MS-SQL
Edit 1: I had worked with RoleManager before in MVC 5, and never got this kind of error. Don't know why I get this error.