How to rename a database column in Entity Framework 5 Code First migrations without losing data?

Dante picture Dante · Oct 28, 2012 · Viewed 38k times · Source

I got the default ASP.NET MVC 4 template successfully running with EF 5.0 Code First Migrations. However, when I update a model property name, the corresponding table column data is dropped by EF 5.0.

Is it somehow possible to rename the table column without dropping data in an automated way?

Answer

Josh Gallagher picture Josh Gallagher · Nov 29, 2012

Manually edit the Up and Down methods of the migration to use the RenameColumn method to replace the AddColumn and DropColumn that it automatically generates for you.