在IdentityRole上创建日期时间转换错误?

时间:2014-01-24 10:15:57

标签: c# asp.net entity-framework asp.net-mvc-5

我在创建IdentityRole时遇到了这个奇怪的错误。 在我的数据库初始化程序类中我有

   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");
        }
    }

当我第一次运行程序时,会调用InitializeDatePickerDbForEf方法。 在执行以下代码行roleManager.Create(new IdentityRole("Admin"));期间 我收到的错误是:{"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."}

创建角色时如何获取日期时间转换错误?错误是否受到前一行代码的影响? (猜不是)

Visual Studio版本:2013 Asp.Net MVC版本:5.1 数据库:MS-SQL

enter image description here enter image description here

编辑1 :我之前在MVC 5中使用过RoleManager,从未遇到过这种错误。不知道为什么我会收到这个错误。

1 个答案:

答案 0 :(得分:0)

这通常意味着表中的DateTime列正在更新,并且正在从尝试更新它的对象中提供无效日期。请注意,“无效日期”包括SQL Server最小日期值之前的日期,类似于“1899年12月31日”。如果您尝试将DateTime.MinValue(未初始化的DateTime字段的默认值)写入SQL Server,您将收到此错误。确保您从IdentityRole对象(或任何子表/对象)提供给IdentityRole表的日期和日期是有效的SQL Server dateTime。

干杯 -