Skip to content

Commit

Permalink
Document HasColumnOrder
Browse files Browse the repository at this point in the history
Fixes #3469
  • Loading branch information
bricelam committed Oct 26, 2021
1 parent cebe7f3 commit ffb48dc
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
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
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; }
}
}

0 comments on commit ffb48dc

Please sign in to comment.