Search code examples
c#entity-frameworkentity-framework-4ef-code-firstentity-framework-migrations

Reorder columns with EF 4.3 Code-First Migrations


Reading both walkthroughs here:

http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx

http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx

I couldn't find a way to specify the order of the columns in the SQL data table. Looks like the last added property is always mapped to the last column.

Example:

public class SampleData {
    public int Id { get; set; }
    public string Data2 { get; set; }
    public string Data3 { get; set; }
}

Will create a table with the column order:

Id | Data2 | Data3

Updating the class like:

public class SampleData {
    public int Id { get; set; }
    public string Data1 { get; set; }
    public string Data2 { get; set; }
    public string Data3 { get; set; }
}

Then running update will update the table into:

Id | Data2 | Data3 | Data1

Anyone knows if there's a way to specify the column order when using migrations?


Solution

  • There is no way except creating new table and moving all data from the old table to new one. The order of columns in the database is not important - you have your application and UI to order columns as you need.