I’m supporting an application in which we need to migrate the code first generated database. The change is to modify three properties (all on the same table/entity) from a string to a nullable decimal.
Part of the requirement of doing this is that I need to output the changes to a SQL file, since we a deploying the patch to our client, who is also hosting the product in production.
I was told that this is possible but I am unsure how to do it.
Question: How can I, using EF code first, migrate the database table to have nullable decimals instead of strings and have the changes outputted to a SQL file. I am making the assumption that all the values currently in the column are convertible to decimals, but if not how would that change the complexity?
If I’m correct in the requirements, then you’ll need to create a migration, then run
This will create a sql script that will be run against the DB to update the structure.
Also
Update-Database -Verbosewill update the DB structure, and output the SQL run.If you need to preserve the data, you can also run SQL in your migration script directly:
And there you can do whatever you need to do to preserve the data: add temp colum/table, copy original data there, change the column type, convert the data to a required format and copy it to the new column, clean-up.
If you have Sql command in the migration, when you run
Update-Database -Script, it will give you combined script – scaffolded migration script and your manually written script.