10

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?

4
  • a) There is no way to insert into previous columns without doing a lot of hacking, b) you shouldn't be keeping deprecated data in your database by using "soft-deletes" - look into archiving deprecated data. Commented Mar 27, 2012 at 20:22
  • What is the point of having columns in database table in specific order? It is just a data storage. Commented Mar 28, 2012 at 7:24
  • 1
    Just for aesthetic purposes, that's all. You can specify it when the database is created with the model builder so just wondered if it's possible when adding a new column as part of a migration. No big deal. Commented Mar 28, 2012 at 8:31
  • 1
    For me, this is a huge issue. I'm setting up a Code First model to match an existing (huge) database. The CF tools don't allow you to enforce an order on the newly-built database, and the Schema Compare tool does not appear to allow you to ignore column order. With hundreds of tables, it will take me days instead of hours. Commented Aug 13, 2012 at 22:38

1 Answer 1

6

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 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.