If EF migration
decides to rename a column
(or a table
), it fulfills this by dropping the (old) column and then adding a column with the new name. This clearly leads to data loss.
Is there any way to prevent EF migration
from dropping a column and force it to use RenameColumn
instead?
Well, I didn't find a clear and straightforward solution.
My solution is hiding
DbMigration
class which every migration that was generated using code-based migration is derived from. I do this by introducing a new class with the same name (DbMigration
). Then I put it inside the same assembly and same namespace where code files reside. This way, any reference of code files to the originalDbMigration
resolves to the newDbMigration
type. Then, I can switch dropping a column ON or Off:And in a code file: