EntityFramework 代码优先:设置字段顺序 [英] EntityFramework code first: Set order of fields

查看:50
本文介绍了EntityFramework 代码优先:设置字段顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 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

2017 年 7 月 12 日更新

此问题在 Backlog 里程碑中.这意味着它不是将在 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.

截至 2019 年 6 月 10 日更新

问题 2272 随 EF Core v2.1 一起提供,并将生成的表中列的顺序与类中的属性顺序进行匹配.但是,正如@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆