Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 747
SteveB Avatar asked Mar 27 '12 20:03

SteveB


1 Answers

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

  • How To change the column order of An Existing Table in SQL Server 2008
  • SQL SERVER – Change Order of Column In Database Tables
  • https://dba.stackexchange.com/questions/61978/how-to-change-the-column-order
like image 172
Stephen Reindl Avatar answered Oct 24 '22 10:10

Stephen Reindl