我在迁移时使用 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
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.
问题 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/