Skip to content

Column ordering #2272

Closed
Closed

Description

@anpete

Outcome from much discussion on this topic...

In EF6 we tried having column order match the order of properties in the class. The issue is that reflection can return a different order on different architectures. Although the order of properties in the model does not theoretically matter... it actually created a lot of very difficult to debug issues on EF6 where the order had a subtle effect on conventions, model diffing, etc. We managed to fix some of them (after many releases) but there are still some issues we have never really fixed. For that reason, metadata is stored in a deterministic (alphabetical) order in metadata in EF7.

In EF6, we also had a column ordering API, but this only impacts the initial create of the table. If you later change the order, migrations does not handle this (it would require a table rebuild). Also, if you add new columns, they are always appended to the table (since anything else also requires a table rebuild).

We could, however, store an ordering hint as an annotation on the model (based on the reflection order). This would only affect the order that migrations writes the columns in a create table call. That way it doesn't matter if it changes when you run your app on different platforms, since it's just an annotation that is ignored for the most part. If we wanted to, we could also flow an order that was specified in [Column] annotations into the hint field.

Activity

  1. bricelam commented on May 26, 2015

    @bricelam
    Contributor

    Only for keys?

  2. anpete commented on May 26, 2015

    @anpete
    ContributorAuthor

    No, for fine grained control of table layout. See #2269.

  3. added this to the 7.0.0 milestone on May 27, 2015
  4. self-assigned this
    on Jul 3, 2015
  5. ChristineBoersen commented on Aug 9, 2015

    @ChristineBoersen

    Just to add to this, adding the HasColumnOrder attribute isn't currently respected either and instead the columns are added in alphabetical order.

    Is there a way to work around this incomplete feature in beta 6 ?

  6. ErikEJ commented on Aug 9, 2015

    @ErikEJ
    Contributor

    @ChristineBoersen Why do you need to manage the colum order?

  7. ChristineBoersen commented on Aug 9, 2015

    @ChristineBoersen

    There are times you need to write code at the database level and column order can become more important (inserting into a table without full column specs in a trigger for example). On a wide table, it can save time and since columns in sql databases don’t change order once written easily, is usually fairly safe.

    In addition, I prefer to look at the model and data in the same column order 99% of the time so I am not hunting for information visually (example comparing query output debugging a query versus framework output). Maybe it’s a little OCD on my part as well as the consistency just “feels right”.

    That or worst case obsolete the currently non-implemented .HasColumnOrder attribute so people know it is incomplete at least. :)

    Christine

    From: Erik Ejlskov Jensen [mailto:[email protected]]
    Sent: Sunday, August 9, 2015 2:09 PM
    To: aspnet/EntityFramework [email protected]
    Cc: Christine Boersen [email protected]
    Subject: Re: [EntityFramework] Migrations: Need column order annotation/API. (#2272)

    @ChristineBoersen https://github.com/ChristineBoersen Why do you need to manage the colum order?

    —
    Reply to this email directly or view it on GitHub #2272 (comment) . https://github.com/notifications/beacon/ANCv3frvA8bNvpsgfDiBF20GHBH12nS9ks5ol47YgaJpZM4Eqf6c.gif

  8. jmalatia commented on Sep 7, 2015

    @jmalatia

    Just created a db with 7.0.0-beta7-15540 and it created the table columns in alphabetical order... I would expect it to create the table in the same order as the model is written as in EF6. Db Column order is important for review as well as other things.

    See also this issue #412 which I believe references a similar issue.

  9. CodeZombieCH commented on Nov 20, 2015

    @CodeZombieCH

    I strongly agree with @ChristineBoersen and @jdaley (#2269 (comment)). Column order is of importance when you work with SQL management tools. It allows you to group columns that share a logical relation.

  10. rowanmiller commented on Nov 20, 2015

    @rowanmiller
    Contributor

    This one is interesting... in EF6.x you theoretically get the order things are defined in the class but in actual fact it depends on what processor you run on. We saw plenty of cases where the model would change between x86/x64... this causes all sorts of headaches because it can have very subtle effects on conventions etc. and result in a different model between processors.

    The other data point is that column ordering only works for the initial table creation. After that, columns are always added to the end of the table.

    If you want to change the ordering though... you can just re-arrange things in the generated migration. That is explicit, and columns will be created in the order they are listed in the create table call. And we know it won't change, since the API calls are explicitly listed in code.

  11. 112 remaining items

  12. bricelam commented on Aug 24, 2020

    @bricelam
    Contributor

    @amitkandwal416 This issue was resolved in EF Core 2.1.0. Are you looking for issue #10059?

  13. xguan2014 commented on Sep 28, 2020

    @xguan2014

    @premchandrasingh Hi, I tried your solution, but it does not work... HasColumnOrder is not respected... :( do you have any idea. I am on .NET Core 3.1..

  14. XnAndrew commented on Oct 14, 2020

    @XnAndrew

    Best option is to use MySQL... Then your migrations can have Alter table... After" commands. Sadly Microsoft SQL doesn't :(

  15. XnAndrew commented on Oct 14, 2020

    @XnAndrew

    Alternative.. Rebuild the Model from the db... (scaffold with -force) and recreate the initial migration...

  16. pgiacomo69 commented on Oct 16, 2020

    @pgiacomo69

    Today, I'm trying .NET 5 RC2, compiled my Blazor app, update-database to create it, and guess what? The [(Order=n)] Attributes that where in entity fields suddenly worked! (They never did until now)

  17. jemiller0 commented on Oct 16, 2020

    @jemiller0

    I guess miracles really do happen. It only took how many years for this basic functionality?

  18. Matt11 commented on Jun 8, 2021

    @Matt11

    From my experience, with EF 5.0.6 (latest stable version 2021-06-08) - this still does not work (using MS SQL Server).

  19. bricelam commented on Jun 9, 2021

    @bricelam
    Contributor

    @Matt11 Could you clarify what you mean by does not work? Are you looking for #10059?

  20. Matt11 commented on Jun 9, 2021

    @Matt11

    @bricelam - exactly, trying to specify in which order the fields are being created via migration/code first, via [Column(Order = x)] attribute. Despite of specifying it for a table, EF core does not follow this order (as you can see via Sql mgmt studio).

  21. 0deprecated0 commented on Sep 27, 2024

    @0deprecated0

    Any updates about this problem?

    I have a base model and I tried to order those columns, and the order attribute is not respected!

    public abstract class GenericModel : ICloneable
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)] // Auto-incrementing ID
        [Column("id", Order = 0, TypeName = "bigint")]
        public long Id { get; private set; }
        protected void SetId(long id){ Id = id; }
    
        [Required]
        [Column("created_at", Order = 99, TypeName = "datetime")]
        [DataType(DataType.DateTime)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{yyyy-MM-dd HH:mm}")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public DateTime CreatedAt { get; private set; }
    
        [Required]
        [Column("updated_at", Order = 100, TypeName = "datetime")]
        [DataType(DataType.DateTime)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{yyyy-MM-dd HH:mm}")]
        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public DateTime UpdatedAt { get; private set; }
    
        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }

    image

  22. roji commented on Sep 27, 2024

    @roji
    Member

    @melhalees please open a new issue with a minimal, runnable repro.

  23. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

good first issuehelp wanted

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions