EF Core - Table 列自动排序

EF Core - Table columns are automatically sorting

我刚刚向当前使用 EF 代码优先迁移的 .NET Core 应用程序添加了一些新模型。以前的模型已经按照我的模型属性的顺序很好地迁移了,但现在情况似乎发生了变化。新模型首先按键值排序,然后按字母顺序排序。

这是一个例子...

我的记录class:

[Table("Logs")]
public class LogEntry
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public string Id { get; set; }

    public LogEntryType Type { get; set; }

    [StringLength(4000)]
    public string Message { get; set; }

    [StringLength(4000)]
    public string StackTrace { get; set; }

    public DateTime LogTimeUtc { get; set; } = DateTime.UtcNow;
}

我在 VS 2017 中输入我的包管理器控制台:

add-migration InitialCreate -Context LoggingContext

并在迁移构建器中生成此内容:

migrationBuilder.CreateTable(
            name: "Logs",
            columns: table => new
            {
                Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
                LogTimeUtc = table.Column<DateTime>(type: "datetime2", nullable: false),
                Message = table.Column<string>(type: "nvarchar(4000)", maxLength: 4000, nullable: true),
                StackTrace = table.Column<string>(type: "nvarchar(4000)", maxLength: 4000, nullable: true),
                Type = table.Column<int>(type: "int", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Logs", x => x.Id);
            });

注意到属性的顺序已经改变了吗?使用以下命令时,同样的顺序将应用于数据库:

update-database -Context LoggingContext

如何让 EF Core 停止对我的列进行排序?上次我为这个应用程序创建一个新的 table(使用完全相同的方法,2 个月前)它保留了它的顺序,而且我认为从那以后我没有对它做任何事情。

它发生在 2.0、EF Core 2.1 或更新迁移之后,最初以与 classes.Refer 到 here 中声明的属性相同的顺序为表生成列。