Entity-framework – How to affect the column order with Entity Framework Code First Migrations

entity-frameworkentity-framework-4entity-framework-4.3

I'm using Entity Framework 4.3 Code First and trying out the Migrations feature.

If I add a new property to my class and then run Add-Migration from the package manager console window I get something like this:

    public override void Up()
    {
        AddColumn("Products", "Discontinued", c => c.Boolean(nullable: false));
    }

I would like to be able to affect the order of the column as I don't want it to just be appended to the table but rather placed at a specific index. I thought I might be able to add it to my modelBuilder configuration, something like:

Property(p => p.Discontinued).HasColumnOrder(2);

but running Update-database does not appear to use it. Can this be done as a migration?

Best Answer

This is just a matter of missing functionality. SQL by itself does not rely on any implicit order of columns (with some exceptions: ORDER BY , ...).

Neither SQL Server nor ORACLE do have a direct SQL DDL command (aka ALTER TABLE...) to move a column around.

Therefore there's no possibility to change the order without high effort (recreate the table). See for example