c# - EntityFramework 代码首先是 : Set order of fields

标签 c# entity-framework asp.net-core entity-framework-core asp.net-core-1.0

我在迁移时使用 EntityFramework 和“代码优先”方法。

我已经成功地从我的模型生成了表格,但是这些列是按字母顺序添加的,而不是我模型中的顺序。

我试过这个:

[Key, Column(Order=0)]
public int MyFirstKeyProperty { get; set; }

[Column(Order=1)]
public int MySecondKeyProperty { get; set; }

但这似乎不起作用。

如何手动设置数据库中字段的顺序?

我正在使用 ASP.NET Core 和 EF Core (SqlServer) v1.1.0。

最佳答案

目前未实现按类属性对列进行排序。 这是关于列排序的长期讨论。 Column ordering #2272

Update as of 07/12/2017

This issue is in the Backlog milestone. This means that it is not going to happen for the 2.0 release. We will re-assess the backlog following the 2.0 release and consider this item at that time.

Update as of 06/10/2019

问题 2272 随 EF Core v2.1 一起提供,并将生成的表中的列顺序与类中属性的顺序相匹配。但是,正如@lloyd-conrade 提到的,这仅对初始创建有用

已创建一个新问题 #10059 来跟踪尊重 Column 属性的 Order 属性的可能实现。

If the implementation of #2272 is insufficient for you and specifying something like [Column(Order = 1)] would help, please vote for this issue and add details about your scenario (if not already listed) below.

请注意,“Punted for 3.0”标签是在 2019 年 5 月 10 日添加的,也就是说它不会在 EF Core 3.0 中发布。

关于c# - EntityFramework 代码首先是 : Set order of fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41266992/

相关文章:

c# - 定义有界容量的贪婪和非贪婪数据流 block 之间的区别

c# - 字段初始值设定项不能引用非静态字段

c# - 需要将 double 打印为 12.20 而不是 12.2

entity-framework - SQL Server“用户架构分离”和 Entity Framework 问题

asp.net-mvc - Entity Framework Core 1.1.1 连接字符串引发 ArgumentNullException

asp.net-core - 有没有办法使用 Razor Pages 进行动态路由?

azure - 如何在 Azure 中远程调试 net46 ASP.NET Core 应用程序?

c# - 如何在字典 C# 中编辑值数据

c# - 根据属性值映射到类

c# - 如何为 {id}/visit 创建有效路线?