efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Column ordering

Open anpete opened this issue 11 years ago • 105 comments

Outcome from much discussion on this topic...

In EF6 we tried having column order match the order of properties in the class. The issue is that reflection can return a different order on different architectures. Although the order of properties in the model does not theoretically matter... it actually created a lot of very difficult to debug issues on EF6 where the order had a subtle effect on conventions, model diffing, etc. We managed to fix some of them (after many releases) but there are still some issues we have never really fixed. For that reason, metadata is stored in a deterministic (alphabetical) order in metadata in EF7.

In EF6, we also had a column ordering API, but this only impacts the initial create of the table. If you later change the order, migrations does not handle this (it would require a table rebuild). Also, if you add new columns, they are always appended to the table (since anything else also requires a table rebuild).

We could, however, store an ordering hint as an annotation on the model (based on the reflection order). This would only affect the order that migrations writes the columns in a create table call. That way it doesn't matter if it changes when you run your app on different platforms, since it's just an annotation that is ignored for the most part. If we wanted to, we could also flow an order that was specified in [Column] annotations into the hint field.

anpete avatar May 26 '15 16:05 anpete

Only for keys?

bricelam avatar May 26 '15 16:05 bricelam

No, for fine grained control of table layout. See #2269.

anpete avatar May 26 '15 17:05 anpete

Just to add to this, adding the HasColumnOrder attribute isn't currently respected either and instead the columns are added in alphabetical order.

Is there a way to work around this incomplete feature in beta 6 ?

ChristineBoersen avatar Aug 09 '15 17:08 ChristineBoersen

@ChristineBoersen Why do you need to manage the colum order?

ErikEJ avatar Aug 09 '15 18:08 ErikEJ

There are times you need to write code at the database level and column order can become more important (inserting into a table without full column specs in a trigger for example). On a wide table, it can save time and since columns in sql databases don’t change order once written easily, is usually fairly safe.

In addition, I prefer to look at the model and data in the same column order 99% of the time so I am not hunting for information visually (example comparing query output debugging a query versus framework output). Maybe it’s a little OCD on my part as well as the consistency just “feels right”.

That or worst case obsolete the currently non-implemented .HasColumnOrder attribute so people know it is incomplete at least. :)

Christine

From: Erik Ejlskov Jensen [mailto:[email protected]] Sent: Sunday, August 9, 2015 2:09 PM To: aspnet/EntityFramework [email protected] Cc: Christine Boersen [email protected] Subject: Re: [EntityFramework] Migrations: Need column order annotation/API. (#2272)

@ChristineBoersen https://github.com/ChristineBoersen Why do you need to manage the colum order?

— Reply to this email directly or view it on GitHub https://github.com/aspnet/EntityFramework/issues/2272#issuecomment-129222648 . https://github.com/notifications/beacon/ANCv3frvA8bNvpsgfDiBF20GHBH12nS9ks5ol47YgaJpZM4Eqf6c.gif

ChristineBoersen avatar Aug 09 '15 18:08 ChristineBoersen

Just created a db with 7.0.0-beta7-15540 and it created the table columns in alphabetical order... I would expect it to create the table in the same order as the model is written as in EF6. Db Column order is important for review as well as other things.

See also this issue https://github.com/aspnet/EntityFramework/issues/412 which I believe references a similar issue.

jmalatia avatar Sep 07 '15 22:09 jmalatia

I strongly agree with @ChristineBoersen and @jdaley (https://github.com/aspnet/EntityFramework/issues/2269#issue-80748826). Column order is of importance when you work with SQL management tools. It allows you to group columns that share a logical relation.

CodeZombieCH avatar Nov 20 '15 22:11 CodeZombieCH

This one is interesting... in EF6.x you theoretically get the order things are defined in the class but in actual fact it depends on what processor you run on. We saw plenty of cases where the model would change between x86/x64... this causes all sorts of headaches because it can have very subtle effects on conventions etc. and result in a different model between processors.

The other data point is that column ordering only works for the initial table creation. After that, columns are always added to the end of the table.

If you want to change the ordering though... you can just re-arrange things in the generated migration. That is explicit, and columns will be created in the order they are listed in the create table call. And we know it won't change, since the API calls are explicitly listed in code.

rowanmiller avatar Nov 20 '15 23:11 rowanmiller

There is something else that I have noticed (small but annoying) when you scaffold out your model the order in CRUD Views is also alphabetical...

RossAndrewMcLeod avatar Dec 20 '15 05:12 RossAndrewMcLeod

@RossAndrewMcLeod - do you mean when you reverse engineer a model from the database? If so, we have an item to generate them in the same order they are defined in the database https://github.com/aspnet/EntityFramework/issues/4062

rowanmiller avatar Dec 22 '15 18:12 rowanmiller

Just to add to this : in our use case, the development team creates the database with migrations, and another team (more business oriented) has a read access on this database directly through SQL Server Management Studio (they extract business data by querying directly in SQL). For them, it is VERY convenient to have the columns ordered in a "logical" order. We would like to be able to generate the migrations (at least the first migration) directly in this "logical" order, without having to re-arrange things in the generated migrations.

This order could be based:

  • on the Order argument of the Column attribute, if it is provided ;
  • otherwise, in the order of the property in the entity class

(columns with an Order argument explicitely given would always be positionned before)

ghost avatar Jan 06 '16 17:01 ghost

...the order of the property in the entity class...

FYI, this information is lost during compilation. The order of TypeInfo.DeclaredProperties is non-deterministic.

bricelam avatar Jan 06 '16 18:01 bricelam

If I remember correctly last time we discussed this subject we said that we could flow the reflection order (which as pointed out is considered non deterministic) as an annotation that then we could use as a hint when generating the migrations. That would help attain user exectations while not making our models harder to compare or hash. I am surprised not to see any notes about this in the bug, though, so I wonder if I am making it up or if there is another bug tracking that? @rowanmiller do you remember the conversation I am referring to?

divega avatar Jan 06 '16 19:01 divega

I've made this patch to add this feature to 7.0.0-rc1 :

Reorder properties according to Column attribute ('Order' argument).

For an entity:
- Reordering is performed only if at least one property of the
entity has a Column attribute with an 'Order' argument
- Properties without the Column/Order argument are positionned
according to the order of TypeInfo.DeclaredProperties (although
it is non-deterministic, it is considered as a hint of user
expectation).

ghost avatar Jan 06 '16 22:01 ghost

@divega yep, I agree that we should do that. This means we have a hint to give to migrations about the order things should be created in the table... but it's completely independent of the order that metadata is defined in the model. It will give folks what they want in most cases.

rowanmiller avatar Jan 06 '16 22:01 rowanmiller

@rowanmiller Since @sebok has shown interest, do you think we can make this up-for-grabs?

divega avatar Jan 06 '16 22:01 divega

For sure, I'll add some notes to the description of the issue.

rowanmiller avatar Jan 06 '16 22:01 rowanmiller

@rowanmiller, sorry for the late reply. No not when reverse engineering an existing database model. Just that when you use the 'New Scaffolded Item' on a POCO you would expect the items to be layed out in the order they were declared in the model.

RossAndrewMcLeod avatar Jan 12 '16 09:01 RossAndrewMcLeod

@smitpatel talk to @bricelam about how to implement this - we think it can be contained completely within migrations (in the ModelDiffer rather than using annotations)

rowanmiller avatar Feb 01 '16 19:02 rowanmiller

I came up with a work around for this issue in an ASP.NET 5 web application. Before the first migration run the "migrations add" command. Then open the migration file that is generated in the Migrations folder. Arrange the columns in the order you want them in. Then run the "database update" command.

Unfortunately I can't figure out a way to order columns that are added after the first migration. But you can go into MS SQL and reorder those columns. And that order will stick even after updating the same table with subsequent migrations.

ClintBailiff avatar Feb 25 '16 14:02 ClintBailiff