Features Of Entity Framework Core 2.1 - Better Column Ordering In Initial Migration

This article discusses the new features added in Entity Framework Core 2.1.

Before we discuss the feature itself, let's first decide if the order of any column in a table really matters.
 
Yes and No - The first thing to understand is that the logical order of columns in a table has an impact on physical storage. If we have a table without a clustered index (primary key in SQL Server), then the table is a Hash table. If we make any query on the Hash table, the data retrieval (reading) would be based on scanning of the whole row. Additionally, other factors for impact can be the following - the columns types under scan are varchar or the image or text and data is really big (in gigabytes).
 
Note - I could not find any reliable source to support my answer.
 

Entity Framework Core 2.0

 
If you are using an old version, start with a quick recap on how column ordering could be done. There are actually 3 ways.
 
Default - If you do not add any code for an order of column, then the order of columns is alphabetically in the table.
  1. public class Post  
  2. {  
  3.          
  4.        [Key]  
  5.        public int PostId { getset; }  
  6.   
  7.          
  8.        public string Title { getset; }  
  9.   
  10.          
  11.        public int UserId { getset; }  
  12.   
  13.          
  14.        public DateTime CreatedOn { getset; }  
  15.   
  16.          
  17.        public string Content { getset; }  
  18.          
  19.        public DateTime ModifiedOn { getset; }  
  20.   
  21.          
  22.        public int BlogId { getset; }  
  23. }  
The result is as below.
 
New Features Of Entity Framework Core 2.1 - Better Column Ordering In Initial Migration 
 
Manual - we can reorder the column in the scaffolded migration.
  1. migrationBuilder.CreateTable(  
  2.                 name: "Posts",  
  3.                 columns: table => new  
  4.                 {  
  5.                     PostId = table.Column<int>(type: "int", nullable: false)  
  6.                         .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),  
  7.                     Title = table.Column<string>(type: "nvarchar(max)", nullable: true),  
  8.                     UserId = table.Column<int>(type: "int", nullable: false),  
  9.                     CreatedOn = table.Column<DateTime>(type: "datetime2", nullable: false),  
  10.                     Content = table.Column<string>(type: "nvarchar(max)", nullable: true),  
  11.                     ModifiedOn = table.Column<DateTime>(type: "datetime2", nullable: false),  
  12.                     BlogId = table.Column<int>(type: "int", nullable: false)  
  13.                 },  
  14.                 constraints: table =>  
  15.                 {  
  16.                     table.PrimaryKey("PK_Posts", x => x.PostId);  
  17.                 });  
The result is below. Note that now the order of columns is exactly like the order of properties in our Post class above.
 
New Features Of Entity Framework Core 2.1 - Better Column Ordering In Initial Migration
 
Using T-SQL migration
  1. migrationBuilder.Sql("CREATE TABLE [dbo].[Posts](" +  
  2.                "[PostId][int] IDENTITY(1, 1) NOT NULL," +  
  3.                "[Title][nvarchar](max) NULL," +  
  4.                "[UserId][int] NOT NULL," +  
  5.                "[CreatedOn][datetime2](7) NOT NULL," +  
  6.                "[Content][nvarchar](max) NULL," +  
  7.                "[ModifiedOn][datetime2](7) NOT NULL," +  
  8.                "[BlogId][int] NOT NULL," +  
  9.                "CONSTRAINT[PK_Posts_new] PRIMARY KEY CLUSTERED([PostId] ASC))") ;  
The result is exactly like above (1).
 
Using DataAnnotations
  1. public class Post  
  2.     {  
  3.         [Key, Column( Order = 0)]  
  4.         public int PostId { getset; }  
  5.   
  6.         [Column( Order = 1)]  
  7.         public string Title { getset; }  
  8.   
  9.         [Column( Order = 2)]  
  10.         public int UserId { getset; }  
  11.   
  12.         [Column(Order = 3)]  
  13.         public DateTime CreatedOn { getset; }  
  14.   
  15.         [Column(Order = 4)]  
  16.         public string Content { getset; }  
  17.         [Column(Order = 5)]  
  18.         public DateTime ModifiedOn { getset; }  
  19.   
  20.         [Column(Order = 6)]  
  21.         public int BlogId { getset; }  
  22. }  

Entity Framework Core 2.1

 
Now, with new Entity Framework Core 2.1, it is actually very easy. The order of columns is exactly the order of properties in the class.
  1. public class Post  
  2.     {  
  3.         public int PostId { getset; }  
  4.   
  5.   
  6.         public string Title { getset; }  
  7.   
  8.   
  9.         public int UserId { getset; }  
  10.   
  11.   
  12.         public DateTime CreatedOn { getset; }  
  13.   
  14.   
  15.         public string Content { getset; }  
  16.   
  17.         public DateTime ModifiedOn { getset; }  
  18.   
  19.   
  20.         public int BlogId { getset; }  
  21.     }  
The scaffolded migration created automatically is as below.
  1. migrationBuilder.CreateTable(  
  2.                 name: "Posts",  
  3.                 columns: table => new  
  4.                 {  
  5.                     PostId = table.Column<int>(nullable: false)  
  6.                         .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),  
  7.                     Title = table.Column<string>(nullable: true),  
  8.                     UserId = table.Column<int>(nullable: false),  
  9.                     CreatedOn = table.Column<DateTime>(nullable: false),  
  10.                     Content = table.Column<string>(nullable: true),  
  11.                     ModifiedOn = table.Column<DateTime>(nullable: false),  
  12.                     BlogId = table.Column<int>(nullable: false)  
  13.                 },  
  14.                 constraints: table =>  
  15.                 {  
  16.                     table.PrimaryKey("PK_Posts", x => x.PostId);  
  17.                 });  
And, the result is like this.

New Features Of Entity Framework Core 2.1 - Better Column Ordering In Initial Migration
 

Conclusion

 
The new feature in Entity Framework Core 2.1 is really going to save a lot of time for developers and reduce the complexity level for workarounds to get the order of columns in a table.