8

I Have an EF Core 3.1 code first project in which most classes inherit from a common base class named BusinessObject.

public abstract class BusinessObject
{    
    [Required()]
    [Column("Id", Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Column("Comment", Order = 100)]
    public string Comment { get; set; }

    [Required()]
    [Column("CreatedAt", Order = 101)]
    public DateTimeOffset CreatedAt { get; set; } = DateTime.UtcNow;

    [Required()]
    [Column("CreatedByUserId", Order = 102)]
    public int CreatedByUserId { get; set; }           

    //A few more columns....
}

[Table("MyTable", Schema = "SampleSchema")]
public class MyTable: BusinessObject
{
    [Column("MyColumns1", Order = 1)]
    [MaxLength(256)]
    public string MyColumns1{ get; set; }

    [Column("MyColumns2", Order = 2)]
    [MaxLength(256)]
    public string MyColumns2{ get; set; }
}

As you can see, I want to set the order of the columns by data annotations and I would expect a table to be created like this:

  • Id
  • MyColumns1
  • MyColumns2
  • Comment
  • CreatedAt
  • CreatedByUserId

In reality, the migration does

  • Id
  • Comment
  • CreatedAt
  • CreatedByUserId
  • MyColumns1
  • MyColumns2

What am I missing? Is this possible by Fluent API? I prefer data annotations to keep the DBContext lean.

Ned Flanders
  • 449
  • 1
  • 5
  • 18
  • 2
    Take a look at this: https://stackoverflow.com/questions/41266992/entityframework-code-first-set-order-of-fields –  Nov 18 '19 at 00:04
  • Thanks for the info. I wonder why this is not higher prioritized. – Ned Flanders Nov 18 '19 at 01:49
  • What is the reason behind having columns in particular order? – Fabio Nov 18 '19 at 03:25
  • 1
    There is more than one reason. Check out https://stackoverflow.com/questions/894522/is-there-any-reason-to-worry-about-the-column-order-in-a-table – Ned Flanders Nov 18 '19 at 09:32
  • @NedFlanders, from the question you linked: From accepted answer: _So **for recent databases**, column order no longer has any impact_, from another answer: _No, the order of the columns in a SQL database table is totally irrelevant - except for display / printing purposes_ – Fabio Nov 22 '19 at 01:44
  • @Fabio That is a very coder-perspective response, which is common in code-first questions. People who really use databases care about the layout, same as you probably don't have one massive MyClasses.cs file with all your classes running to thousands of lines "because it's totally irrelevant (to the compiler)" – Red Jan 21 '21 at 14:57

2 Answers2

3

Although this is an old thread, this is exactly the same issue that I am having too and may be resolved using the code as suggested in this link here

https://github.com/premchandrasingh/EFCoreColumnOrder

which stems from reported problems on git hub here

https://github.com/dotnet/efcore/issues/2272

It is suggested that this issue is fixed in core 2.0, however there is a proportion of people saying and confirming that this is not the case, thus leading to using the custom implementation.

Simon Price
  • 3,011
  • 3
  • 34
  • 98
3

In NET 5.0 RC2, the “order” attribute works!