简体   繁体   English

EF Core-表格列自动排序

[英]EF Core - Table columns are automatically sorting

I have just added a few new models to my .NET Core application that is currently using EF code-first migrations. 我刚刚在当前使用EF代码优先迁移的.NET Core应用程序中添加了一些新模型。 Previous models have migrated fine and in the order of my model properties, but things seem to have changed now. 以前的模型可以按模型属性的顺序很好地迁移,但是现在情况似乎有所变化。 New models are sorted first by key values, then alphabetically. 新模型首先按键值排序,然后按字母顺序排序。

Here's an example... 这是一个例子

My logging 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;
}

I type into my package manger console in VS 2017: 我在VS 2017中输入我的软件包管理器控制台:

add-migration InitialCreate -Context LoggingContext

and that produces this in the migration builder: 并在迁移构建器中生成以下代码:

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);
            });

Notice that the order of the properties have changed? 注意属性的顺序已更改吗? This same ordering is then applied to the database when using: 使用以下命令时,会将相同的顺序应用于数据库:

update-database -Context LoggingContext

How can I get EF Core to stop ordering my columns? 如何获得EF Core停止订购我的色谱柱? The last time I made a new table for this application (using the exact same method, 2 months back) it retained its order, and I don't think I've done anything to it since. 我上一次为此应用程序创建新表时(使用完全相同的方法,距今已有两个月),它保留了其顺序,从那以后我不做任何事情。

它发生在2.0,EF核心2.1或更新后,迁移到最初生成的表列在同一顺序为属性的声明中classes.Refer到这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM