如何在EF Core 2.1中按顺序排序继承列?

4

我使用EF Core 2.1创建SQL Server数据库,但是数据库列的排序没有将继承列放在最后, 以下是我的实体代码:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

public class RssBlog : Blog
{
    public string RssUrl { get; set; }
}

当前的列排序如下:

1.RssUrl
2.BlodId
3.Url

我希望数据库中的数据如下所示:

1.BlodId
2.Url
3.RssUrl

请问您如何调整数据库列的顺序呢?谢谢!

我是英语初学者,如果我的用词或语句有问题,请见谅。


这是EF Core的一个未解决问题,你可以在GitHub上看到。 - Caldazar
@Caldazar 谢谢!!! - Sparking
2个回答

1
这是我在EF Core 2.1预览版发布时提交的一个开放问题,但目前(版本2.1)尚未修复。
更新于2018年10月6日:
Entity Framework核心团队计划在明年年初的EF Core 3.0版本中修复此问题。
详情请见:https://github.com/aspnet/EntityFrameworkCore/issues/11314

0

在研究解决方案时,我看到了一个技巧。

当您添加迁移时,您将获得以下脚手架迁移文件

migrationBuilder.CreateTable(
                name: "RssBlog",
                columns: table => new
                {
                    BlogId = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    Url = table.Column<string>(nullable: true),
                    RssUrl = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_RssBlog", x => x.BlogId);
                });

表格列的顺序如下:

BlogId
Url
RssUrl

你可以在脚手架迁移文件中重新排序列;

migrationBuilder.CreateTable(
                    name: "RssBlog",
                    columns: table => new
                    {
                        RssUrl = table.Column<string>(nullable: true),
                        BlogId = table.Column<int>(nullable: false)
                            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                        Url = table.Column<string>(nullable: true)
                    },
                    constraints: table =>
                    {
                        table.PrimaryKey("PK_RssBlog", x => x.BlogId);
                    });

重新排序后,表格列的顺序如下:

RssUrl
BlogId
Url

因此,在 ef core 团队发布列顺序功能(相关问题)之前,我们可以像上面那样对列进行排序。

详细博客文章


2
感谢提供一个好的解决方案。虽然这是一个好的解决方案,但如果我有很多实体,仍然需要亲自进行调整,这令人感到麻烦。 - Sparking

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接