I have a BaseEntity
class which includes common fields for all entities where I have specified the column order:
public class BaseEntity
{
[Column(Order = 0)]
public Guid Id { get; set; }
[Column(Order = 1)]
public DateTime CreatedOn { get; set; }
[Column(Order = 2)]
public DateTime UpdatedOn { get; set; }
// other properties
}
I also in some cases need some CreatedBy
and UpdatedBy
fields, so I have this derived class:
public class TraceableEntity : BaseEntity
{
[Column(Order = 3)]
public string? CreatedBy { get; set; }
[Column(Order = 4)]
public string? UpdatedBy { get; set; }
}
My entities are derived from either the BaseEntity
or the TraceableEntity
class, depending on the level of traceability required.
The issue is that when creating migrations EF seemingly ignores the column ordering when there is object inheritance involved. In the DbContextModelSnapshot
, I can see that the column orderings are taken into account, but they seem to follow the inheritance order instead of numerical order. Result is that the CreatedBy
/UpdatedBy
columns appear as column 1 and 2, followed by any other properties, and the CreatedOn
etc show up last.
Is there a way to fix this without manually editing the DbContextModelSnapshot
or re-order the columns?