首先是EntityFramework代码:设置字段顺序 [英] EntityFramework code first: Set order of fields
问题描述
我将EntityFramework与代码优先"的方法一起用于迁移.
I am using EntityFramework with the "Code first" approach with migrations.
我已经成功地从模型中生成了表格,但是列是按字母顺序而不是模型内部的顺序添加的.
I have successfully generated tables from my models, but the columns are being added in an alphabetical order rather than the order inside my model.
我已经尝试过了:
[Key, Column(Order=0)]
public int MyFirstKeyProperty { get; set; }
[Column(Order=1)]
public int MySecondKeyProperty { get; set; }
但这似乎不起作用.
如何手动设置数据库中字段的顺序?
How can I manually set the order of the fields in the database?
我正在使用ASP.NET Core和EF Core(SqlServer)v1.1.0.
I am using ASP.NET Core and EF Core (SqlServer) v1.1.0.
推荐答案
当前未实现按类属性对列进行排序. 这是关于列排序的漫长讨论. 列排序#2272
Currently ordering columns by class property is not implemented. Here's the long discussion about column ordering. Column ordering #2272
Update as of 07/12/2017
此问题在积压里程碑中.这意味着它不是 即将在2.0版本中发布.我们将重新评估积压 遵循2.0版本,并在当时考虑此项.
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
EF Core v2.1附带的问题2272,将生成的表中列的顺序与类中属性的顺序匹配.但是,正如@ lloyd-conrade所述,这仅对初始创建有用
Issue 2272 shipped with EF Core v2.1 and matches the order of the columns in the generated table to the order of the properties in the class. However, as @lloyd-conrade mentioned, this is only useful for initial creation
已创建一个新问题#10059,以跟踪遵守Column属性的Order属性的可能实现.
A new issue, #10059, has been created to track the possible implementation of respecting the Column attribute's Order property.
如果#2272的实现对您来说还不够,请指定一些内容 像[Column(Order = 1)]会有所帮助,请对此问题进行投票并添加详细信息 有关您的方案(如果尚未列出)的信息.
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中发布.
Note the "Punted for 3.0" label was added on May 10th, 2019, which is to say it will not ship in EF Core 3.0.
这篇关于首先是EntityFramework代码:设置字段顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!