Skip to content

Allow defining column order via [ColumnAttribute.Order] when creating tables #10059

Closed
Listed in 3 issues
@bricelam

Description

@bricelam
Contributor

#2272 matched the CLR reflection order for columns within a table.

During design, we decided to wait for more feedback before honoring the value of ColumnAttibute.Order on the properties.

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.

Activity

added this to the Backlog milestone on Oct 13, 2017
yitzchokneuhaus1

yitzchokneuhaus1 commented on May 9, 2018

@yitzchokneuhaus1

@bricelam Is this enhancement scheduled for the near future to be implemented in either EF Core or EF6?

bricelam

bricelam commented on May 9, 2018

@bricelam
ContributorAuthor

It works in EF6. Still on the Backlog of EF Core.

samconn

samconn commented on May 30, 2018

@samconn

+1 for implementing this feature, and sooner rather than later.

We're porting an SAAS application from EF6x to EFCore and were a wee bit taken aback to find that the Order designation on the ColumnAttribute is being ignored.

But kudos to the whole team (and community!) for at least going open-kimono on the design discussions to at least help us understand the reasoning.

bricelam

bricelam commented on Jun 18, 2018

@bricelam
ContributorAuthor

@andytaw mentioned another good case for this:

Column order is of no great significance if all data operations go through EF.
I need to use SQL Bulk Copy as well as EF.
I could (will...) write a whole bunch of tedious code to manage the two, however I'd much prefer to use .HasColumnOrder().

MelGrubb

MelGrubb commented on Jul 12, 2018

@MelGrubb

Those who work in the database all day tend like their columns in decreasing order of "importance", with the most significant or often-used coming at the start, and picky details way over on the right out of the way, or sometimes they like them clustered in neighborhoods where related fields are next to each other. Either way, this means that the column order can be significant to some team members. Meanwhile, over in the land of code, I like to use tools like ReSharper or CodeMaid to automatically sort my C# code, which drastically reduces merge collisions. Really, I can't recommend it enough for team projects, it's a huge gain in productivity.

As it stands now, I can't make both sets of developers happy. I really need a way to specify the order of the database columns, and it seems to be that using the existing DataAnnotations' ColumnAttribute would be an ideal way to do this.

As for strategy, I'd recommend putting the properties with the attribute first, in increasing order according to the Order property of the attribute, and then the remaining properties in their existing "natural" order, although that order is non-deterministic and shouldn't be relied upon to remain stable in the future.

pwen090

pwen090 commented on Sep 20, 2018

@pwen090

@bricelam you mentioned it on #2272 but to reiterate and upvote here if we have something like:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

public class RepoBlog : Blog
{
    [Key]
    public int Id { get; set; }
}

This will cause the primary key Id to be output as the last column. I know we can edit migration files but surely somewhere along the lines someone will forget to do that and make it a pain to clean up. It would be great if we can either support Order attribute or at least have the generators try harder to always put key columns first.

bricelam

bricelam commented on Sep 20, 2018

@bricelam
ContributorAuthor
smitpatel

smitpatel commented on Sep 21, 2018

@smitpatel
Contributor

Afaik the model posted by @pwen090 wouldn't work if Blog is mapped since derived types cannot have keys.

92 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

    Development

    Participants

    @MelGrubb@bricelam@ajcvickers@smitpatel@roji

    Issue actions

      Allow defining column order via [ColumnAttribute.Order] when creating tables · Issue #10059 · dotnet/efcore