question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Migrations: Order columns of abstract base class properties last in CreateTable

See original GitHub issue

Before the release of Entity Framework Core 2.1, Entity Framework Core team announced that,

Column ordering in migrations: Based on customer feedback, we have updated migrations to initially generate columns for tables in the same order as properties are declared in classes.

but unfortunately, it is not working as expected in case of the following scenarios:

public abstract class AuditModel
{
    public int? CreatedBy { get; set; }
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public int? ModifiedBy { get; set; }
    public DateTime? ModifiedAt { get; set; }
    public bool IsActive { get; set; } = true;
}

public class Employee : AuditModel
{
   [Key]
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public long EmployeeId { get; set; }

   public string EmployeeNo { get; set; }

   [ForeignKey("Company")]
   public long CompanyId { get; set; }

   [ForeignKey("CompanyOffice")]
   public long OfficeId { get; set; }

   [ForeignKey("Section")]
   public long SectionId { get; set; }
    ..........................
}

Entity Framework Core Migration is generating the Employee Table columns as the following order:

  [CreatedBy] <-----
  ,[CreatedAt] <-----
  ,[ModifiedBy] <-----
  ,[ModifiedAt] <-----
  ,[IsActive] <-----
  ,[EmployeeId]
  ,[EmployeeNo]
  ,[CompanyId]
  ,[OfficeId]
  ,[SectionId]
  ............

Actually It should have been:

  ,[EmployeeId]
  ,[EmployeeNo]
  ,[CompanyId]
  ,[OfficeId]
  ,[SectionId]
   ............
  ,[CreatedBy] <-----
  ,[CreatedAt] <-----
  ,[ModifiedBy] <-----
  ,[ModifiedAt] <-----
  ,[IsActive] <-----

Note that, Entity Framework 6.x Migration generates the columns as expected order even in case of inheritance.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:24
  • Comments:61 (17 by maintainers)

github_iconTop GitHub Comments

25reactions
bricelamcommented, Sep 18, 2018

Please upvote #10059 for [Column(Order = 1)] support.

21reactions
ajcvickerscommented, Mar 22, 2018

Triage decisions:

  • Primary keys first
  • Un-mapped base classes at end
Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring abstract base class without creating table in EF ...
My expectation is to create a single Student table with all the BaseEntity fields as columns. How to achieve it? I am using...
Read more >
Inheritance - EF Core
How to configure entity type inheritance using Entity Framework Core. ... Each table contains columns for all properties on the ...
Read more >
Migrate your Room database
Learn to migrate databases safely using the Room Library. ... Add your Migration classes to your database builder using the addMigrations() method:.
Read more >
EFCore and class inheritance - Mario Mucalo's Blog
I was playing with how Entity Framework Core deals with class inheritance in the code first approach – what tables it generates and...
Read more >
How to Work With Inheritance in Entity Framework Core
Properties of any abstract base type are created as fields in the table of each concrete type. The class design will be the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found