2

We use Entity Framework 6 code first. We recently decided to move datetime format in the database to datetime2 format. Our database currently contains lot of data and about 262 tables.

By googling it, I found this answer which describs how to migrate to datetime2:

modelBuilder.Properties<DateTime>()
.Configure(c => c.HasColumnType("datetime2"));

So I put this portion of code in the OnModelCreating method. Then I ran Add-Migration commend which created an Up method containing lots of DropPrimaryKey, AlterColumn and AddPrimaryKey rows, as in this example:

enter image description here

Then I ran Update-Database commend and got this error message:

The object 'DF__AdminMessa__Date__353DDB1D' is dependent on column 'Date'.

ALTER TABLE ALTER COLUMN Date failed because one or more objects access this column.

I guess this error caused as a result of the constrains over the tables. I know I can temporary disable them and run this script again but then we'll have to run the script, that disable the constrains, manually on ~262 tables. Besides that, I don't know if that's OK to run pure script like that while using Code First.

Is there any proper way to migrate existing database (with data) to datetime2 in EF Code First?

Community
  • 1
  • 1
Jacob
  • 3,598
  • 4
  • 35
  • 56

0 Answers0