0

I'm using code first migrations with entity framework. I've got a column that was created a few migrations ago called Amount which is marked as 'required', that stores an integer value. I've now been told, this needs to be a decimal. When I change the datatype in my context and run 'Add-Migration, it creates the migration file as I expect.

When I run Update-Database, it gives me the following error

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

I've managed to write some custom SQL to do this manually:

ALTER TABLE Products
DROP CONSTRAINT [DF__Products__ProductsTrack__522F1F86]
GO
ALTER COLUMN Amount decimal(18,2)

However, this is no good as I need the datatype changed as part of my migrations. I am write in assuming the dependency is because of the 'Required' attribute that was initially assigned to it? How do I change the type using 'normal' migration techniques?

  • You could modify the migration code manually and add the DROP CONSTRAINT part in a [`Sql()`](http://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigration.sql%28v=vs.103%29.aspx) call. – Gert Arnold Sep 12 '13 at 20:13
  • The constraint seems to have generated different names as it's been deployed to different environments, so I can't use the script above as when Update-Database is run on dev environment, it won't find it. – user2743684 Sep 12 '13 at 21:00
  • 1
    Maybe [this](http://stackoverflow.com/questions/11974439/changing-column-default-values-in-ef5-code-first) helps? – Gert Arnold Sep 12 '13 at 21:04

0 Answers0