EF The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

StackOverflow https://stackoverflow.com/questions/23675980

  •  23-07-2023
  •  | 
  •  

Question

I Have read more similar questions as this...

I have created a BD but after to creation I thought to add a DateTime field to save the datetime registration, but when I type update-database (after creation the migration), the Package Manager Console show me the error of the title

I already tried this:

AlterColumn("dbo.Dispositivoes", "RegisterDate", c => c.DateTime(nullable: false, defaultValueSql: "GETDATE()"));
AlterColumn("dbo.Dispositivoes", "RegisterDate", c => c.DateTime(nullable: false, defaultValue: DateTime.Now));

new DataColumn("myDate", typeof(DateTime));

This show ever the same error. I do not have idea to resolve this, any help me please?

Thanks!!

Was it helpful?

Solution

have you tried

Sql("alter table dbo.Dispositivoes alter column RegisterDate datetime2 not null default getdate()")

or

AlterColumn("dbo.Dispositivoes", "RegisterDate", c => c.DateTime(nullable: false, storeType: "datetime2", defaultValueSql: "GETDATE()"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top