【问题标题】:EF Core - Table columns are automatically sortingEF Core - 表列自动排序
【发布时间】:2019-05-18 10:50:41
【问题描述】:

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

这是一个例子......

我的日志类:

[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 停止对我的列进行排序?上次我为这个应用程序创建一个新表时(使用完全相同的方法,2 个月前)它保留了它的顺序,我认为从那以后我没有对它做任何事情。

【问题讨论】:

  • 看起来这仍然是open 问题:github.com/aspnet/EntityFrameworkCore/issues/10059
  • 这个问题看起来更像是一个增强请求。我希望我的问题要么是我找不到的错误配置设置,要么是我没有找到解决方法的最近更新“功能”(除了像我目前需要的那样手动更新迁移脚本之外) )。
  • 它发生在 2.0 中,在 EF Core 2.1 更新迁移以最初为表生成列的顺序与在类中声明属性的顺序相同。请参阅 here
  • @XingZou 好皮卡​​!这就是问题所在。从提交来看,几个月前因为“兼容性问题”而降级。只是在本地更改了版本,它工作正常。圣诞假期过后,我需要与我的团队讨论降级的原因。发表您的评论作为答案,我会将其标记为已接受:)

标签: c# database entity-framework-core asp.net-core-2.0


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-17
    • 2018-07-05
    • 2018-04-05
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多