Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Created Issue: Migration from DateTime to DateTimeOffset column with data fails, because we don't drop default constrant on the datetime column, before runnig ALTER database [678]

$
0
0
If we have a database with DateTime property (datetime column) and we want to change the type to DateTimeOffset using migrations the following error with show up:

The object 'DF__Issues__CreatedT__15502E78' is dependent on column 'CreatedTime'.
ALTER TABLE ALTER COLUMN CreatedTime failed because one or more objects access this column.

The sql that we are trying to execute looks like this:
ALTER TABLE [dbo].[Issues] ALTER COLUMN [CreatedTime] [datetimeoffset](7) NOT NULL

However if one does the same operation using Designer in SQL Server Object Explorer (part of VS), the code they execute looks like this:

ALTER TABLE [dbo].[Issues] DROP CONSTRAINT [DF__Issues__CreatedT__15502E78];
ALTER TABLE [dbo].[Issues] ALTER COLUMN [CreatedTime] DATETIMEOFFSET (7) NOT NULL;
ALTER TABLE [dbo].[Issues] ADD DEFAULT ('1900-01-01T00:00:00.000') FOR [CreatedTime];

Pretty much dropping the default constraint, altering the database and then re-creating the default constraint. We should mimic that behavior if possible, otherwise Migrations experience is broken for this scenario.

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>