Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

I have tried this:

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

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

But that doesn't seem to be working.

How can I manually set the order of the fields in the database?

I am using ASP.NET Core and EF Core (SqlServer) v1.1.0.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
222 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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

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

A new issue, #10059, has been created to track the possible implementation of respecting the Column attribute's Order property.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...