Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document HasColumnOrder #3510

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions entity-framework/core/modeling/entity-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,24 @@ You can set an arbitrary text comment that gets set on the database column, allo
[!code-csharp[Main](../../../samples/core/Modeling/EntityProperties/FluentAPI/ColumnComment.cs?name=ColumnComment&highlight=5)]

***

## Column order

> [!NOTE]
> This feature was introduced in EF Core 6.0.

By default when creating a table with [Migrations](../managing-schemas/migrations/index.md), EF Core orders primary key columns first, followed by properties of the entity type and owned types, and finally properties from base types. You can, however, specify a different column order:

### [Data Annotations](#tab/data-annotations)

[!code-csharp[](../../../samples/core/Modeling/EntityProperties/DataAnnotations/ColumnOrder.cs#snippet_ColumnAttribute)]

The Fluent API can be used to override ordering made with attributes, including resolving any conflicts when attributes on different properties specify the same order number.

### [Fluent API](#tab/fluent-api)

[!code-csharp[](../../../samples/core/Modeling/EntityProperties/FluentAPI/ColumnOrder.cs#snippet_HasColumnOrder)]

***

Note that, in the general case, most databases only support ordering columns when the table is created. This means that the column order attribute cannot be used to re-order columns in an existing table.
2 changes: 0 additions & 2 deletions samples/core/Miscellaneous/NewInEFCore6/ColumnOrderSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public class Address

public static class UsingModelBuilder
{
#region WithOrdering
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @ajcvickers
Copy and pasted from above. Unfortunately, this is the one currently being rendered on What's New.

public class EntityBase
{
public int Id { get; set; }
Expand Down Expand Up @@ -139,7 +138,6 @@ public class Address
public string City { get; set; }
public string Postcode { get; set; }
}
#endregion
}

public class EmployeeContext : DbContext
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace EFModeling.EntityProperties.DataAnnotations.ColumnOrder
{
internal class MyContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}

#region snippet_ColumnAttribute
public class EntityBase
{
[Column(Order = 0)]
public int Id { get; set; }
}

public class PersonBase : EntityBase
{
[Column(Order = 1)]
public string FirstName { get; set; }

[Column(Order = 2)]
public string LastName { get; set; }
}

public class Employee : PersonBase
{
public string Department { get; set; }
public decimal AnnualSalary { get; set; }
}
#endregion
}
43 changes: 43 additions & 0 deletions samples/core/Modeling/EntityProperties/FluentAPI/ColumnOrder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore;

namespace EFModeling.EntityProperties.FluentAPI.ColumnOrder
{
internal class MyContext : DbContext
{
public DbSet<Employee> Employees { get; set; }

#region snippet_HasColumnOrder
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>(x =>
{
x.Property(b => b.Id)
.HasColumnOrder(0);

x.Property(b => b.FirstName)
.HasColumnOrder(1);

x.Property(b => b.LastName)
.HasColumnOrder(2);
});
}
#endregion
}

public class EntityBase
{
public int Id { get; set; }
}

public class PersonBase : EntityBase
{
public string FirstName { get; set; }
public string LastName { get; set; }
}

public class Employee : PersonBase
{
public string Department { get; set; }
public decimal AnnualSalary { get; set; }
}
}