1

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

enter image description here enter image description here

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.

Community
  • 1
  • 1
Cybercop
  • 8,475
  • 21
  • 75
  • 135
  • Which Database Used here – Praveen S Jan 24 '14 at 10:20
  • Post Your Database design pls – Praveen S Jan 24 '14 at 10:22
  • Do I have to? I have nothing to do with IdentityRole and RoleManager in MVC5 it is default. I just use the methods to create role. Also, I'm using code first approach, and it doesn't have any model class for roles. – Cybercop Jan 24 '14 at 10:25
  • http://stackoverflow.com/questions/7386360/the-conversion-of-a-datetime2-data-type-to-a-datetime-data-type-resulted-in-an-o Go through this link u got answer.. – Praveen S Jan 24 '14 at 10:29

1 Answers1

0

This usually means that there is a DateTime column in the table being updated and it is being fed an invalid date from the object trying to update it. Note that an Invalid Date includes dates prior to the SQL Server minimum date value which is something like '31 Dec 1899'. If you try to write a DateTime.MinValue (the default value of an uninitialised DateTime field) to SQL Server you will get this error. Make sure and Dates you are providing to the IdentityRole table from the IdentityRole object (or any child table/objects) is a valid SQL Server dateTime.

Cheers -

simon at rcl
  • 7,326
  • 1
  • 17
  • 24