问题
I used EF Core 2.1 to create SQL Server database, but database columns ordering doesn't put inheritance columns in the last, the following is my entities code:
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
public class RssBlog : Blog
{
public string RssUrl { get; set; }
}
Current columns ordering are:
1.RssUrl
2.BlodId
3.Url
I want it in database like this:
1.BlodId
2.Url
3.RssUrl
Could you tell me how to fix the database columns ordering? Thanks!
I'm beginner for English, if my words or sentences have some problems, I'm so sorry.
回答1:
This is an open issue of Entity Framework Core that I have submitted while EF Core 2.1 Preview 1 released but has not been fixed yet (current version 2.1).
Update On 06-Oct-2018
Entity framework core team has planned to fix this issue in EF Core 3.0 release at the start of next year.
Here is the details: https://github.com/aspnet/EntityFrameworkCore/issues/11314
回答2:
A trick that I see while researching a solution to the problem.
When you add migration you will get following scaffolded migration file
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);
});
Table columns order result is below;
BlogId
Url
RssUrl
You can reorder the columns in the scaffolded migration file;
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);
});
After we reorder, table columns order like below;
RssUrl
BlogId
Url
So before ef core team publish column order feature (related issue) we can order our columns like above.
Detailed blog post
来源:https://stackoverflow.com/questions/51443459/how-to-sort-inheritance-columns-ordering-in-ef-core-2-1