本文介绍了首先是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年12月12日开始更新

Update as of 07/12/2017

从2019年6月10日开始更新

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.

请注意,"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代码:设置字段顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:05