efcore icon indicating copy to clipboard operation
efcore copied to clipboard

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

Open tanvirarjel opened this issue 8 years ago • 60 comments

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.

tanvirarjel avatar Mar 17 '18 18:03 tanvirarjel

@TanvirArjel Can you be more specific about what is "wrong" with the order generated? For example, is it just that the primary key is not the first column? Or is it also that the unmapped base type should go at the end? If the latter, then what is the logic that determines this and would that also be true if it was a mapped base type?

ajcvickers avatar Mar 19 '18 19:03 ajcvickers

@ajcvickers Yes! Primary key should be the first column and the unmapped based type columns should go at the end..

tanvirarjel avatar Mar 19 '18 21:03 tanvirarjel

@TanvirArjel Can you provide an explanation as to why the unmapped base type should go at the end and whether or not this should also be the case for a mapped base type? (I'm not saying you're wrong, I'm just trying to understand what the reasoning is behind this ordering.)

ajcvickers avatar Mar 19 '18 21:03 ajcvickers

Dear @ajcvickers , Thanks for your persistent reply.

Does it make sense that the primary key of the table at column no. 5 or so other than the first column of the table? Look at the two orders of the columns in the question. Which makes sense? Which is more meaningful? Obviously not the first order..Hope,you will also be agree with me.

In case of unmapped based type, yes! it also be more readable and rational that unmapped based type columns should go at the end of the table because normally in the common unmapped base type we put the columns which are common to the every table like CreatorName, CreationTime, ModifierName, ModificationTime etc. These columns at the beginning of the table does not make sense.

Point to be noted that, In Entity Framework 6.2.0, Primary key of the table is always the first column and both unmapped and mapped based type goes at the end. This really makes sense!

I know Entity Framework Core is a breaking change from Entity Framework but what's wrong to bring these already accustomed,well tested and more sensible behaviors from Entity Framework to Entity Framework Core?

tanvirarjel avatar Mar 20 '18 08:03 tanvirarjel

@TanvirArjel Thanks for the additional information. It will help us decide our course of action here.

ajcvickers avatar Mar 20 '18 16:03 ajcvickers

Triage decisions:

  • Primary keys first
  • Un-mapped base classes at end

ajcvickers avatar Mar 22 '18 02:03 ajcvickers

Dear @ajcvickers, Thanks a lot! for taking speedy action..That's why we love and rely on Microsoft :)

tanvirarjel avatar Mar 22 '18 04:03 tanvirarjel

Any update about this?

shargon avatar Aug 11 '18 07:08 shargon

@TanvirArjel This issue is in the 3.0 milestone. This means it is currently planned to be fixed for the 3.0 release.

ajcvickers avatar Aug 13 '18 16:08 ajcvickers

I also would love to have the possibility to work with order annotations eg:


 public class BaseModel
    {
        [Key, Column(Order = 0)]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Column(Order = 100, TypeName = "datetime")]
        public DateTime Base_Addeddate { get; set; }
        [Column(Order = 101, TypeName = "datetime")]
        public DateTime Base_Modifieddate { get; set; }
        [Column(Order = 102, TypeName = "nvarchar(50)")]
        public string Base_Username { get; set; }
        [Column(Order = 103)]
        [MaxLength(255)]
        public string Base_Ipaddress { get; set; }
        [Column(Order = 104)]
        public bool? Base_Enabled { get; set; }
    }

public class Language : BaseModel
    {
        [Column(Order = 2)]
        public string Code { get; set; }
        [Column(Order = 3)]
        public string Name { get; set; }
        [InverseProperty("Language")]
        public virtual IList Countries { get; set; }
    }

Will this be possible? AS currently the order is created is by Name! and ignores totally this Annotation!

So the idea is to have the order as described even by inherited classes!

nunorelvao avatar Sep 17 '18 13:09 nunorelvao

@nunorelvao Think! you have hundred of model classes with lots of columns. Then imagine how many times you have to write order annotations.

tanvirarjel avatar Sep 18 '18 03:09 tanvirarjel

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

bricelam avatar Sep 18 '18 17:09 bricelam

Primary Keys first fixed in #13678

smitpatel avatar Oct 19 '18 19:10 smitpatel

Is there any further work being doing to allow the order of the fields to be set? I have an SoftDeleted class with fields to handle logical deletion and it is inherited by any any class that would like this feature. The problem: the fields from the inherited class are always shown immediately after the primary key field. I understand that I can control the order of the fields within a class, but I cannot across an inherited class.

    public class SoftDeleted : ISoftDeleted
    {
        public DateTime? DeletedOn { get; set; }
        public String DeletedBy { get; set; }
    }

Just a thought: is it possible to have an annotation [PriorityOrder(n)] so you can control the order of the column generation? ex: to

     [PriorityOrder(300)]
    public class SoftDeleted : ISoftDeleted
    {
        public DateTime? DeletedOn { get; set; }
        public String DeletedBy { get; set; }
    }
    [PriorityOrder(100)]
    public class MyClass: SoftDeleted
    {
        [Key]
        public int Id { get; set; }
        public String Comment{ get; set; }
    }

so the migration output would look like

       protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "MyClass",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Comment= table.Column<string>(nullable: true),
                    DeletedOn = table.Column<DateTime>(nullable: true),
                    DeletedBy = table.Column<string>(nullable: true),
                    

instead of:

        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "MyClass",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    DeletedOn = table.Column<DateTime>(nullable: true),
                    DeletedBy = table.Column<string>(nullable: true),
                    Comment= table.Column<string>(nullable: true),

TrevorFradsham avatar Jan 24 '19 13:01 TrevorFradsham

@TrevorFradsham You would likely be able to do what you want with the Column attribute. Issue #10059 is tracking this.

ajcvickers avatar Jan 24 '19 17:01 ajcvickers

thanks for the link. I am not seeing that there is any progress for Column order for EF Core. Will keep monitoring though.

TrevorFradsham avatar Jan 24 '19 19:01 TrevorFradsham

+1 for the explicit orderable properties/columns.

biliktamas79 avatar Feb 15 '19 13:02 biliktamas79

You're doing it wrong. We only count 👍 reactions on #10059. (joking, but kinda true)

bricelam avatar Feb 27 '19 23:02 bricelam

@ajcvickers as you said:

Triage decisions:

Primary keys first Un-mapped base classes at end

Primary keys first has been fixed! What about the Un-mapped base classes at end? Any update please?

tanvirarjel avatar Apr 23 '19 17:04 tanvirarjel

@TanvirArjel The issue is currently in the 3.0 milestone, but 3.0 is still overbooked with work, so unfortunately this issue may have to be pushed back to a later release.

ajcvickers avatar Apr 24 '19 15:04 ajcvickers