Migrations: Order columns of abstract base class properties last in CreateTable
See original GitHub issueBefore 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:
- Created 6 years ago
- Reactions:24
- Comments:61 (17 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Please upvote #10059 for
[Column(Order = 1)]
support.Triage decisions: