You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Migrations: Order columns of abstract base class properties last in CreateTable #11314
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:
changed the title [-]Column order broken in migration while class inheriting few column sfrom another class[/-][+]Column order broken in migration while model class inheriting few columns from another class[/+]on Mar 17, 2018
@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?
@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.)
Reacted by Timo van Zijll Langhout
Reacted by dnxit, Everton Nogueira, Anton Urbaník and Yesehake Berhe Aka Isaac
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?
Reacted by Jeff Moerdyke, Prakash J, Colin Wynn, Cem K, Luis Palacios, Aneesh Gopalakrishnan, oof, surasak-rit and joshkautz
Nobody here has any expectations about unmapped types in the middle of a hierarchy, right? This is just about the case identified in the issue description?
This doesn't work with MySQL. Any thoughts?
here's the error I'm getting:
System.InvalidOperationException: The current migration SQL generator 'CustomSqlServerMigrationsSqlGenerator' is unable to generate SQL for operations
of type 'Microsoft.EntityFrameworkCore.Migrations.Operations.MySqlCreateDatabaseOperation'.
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder
builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlDatabaseCreator.CreateCreateOperations()
at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlDatabaseCreator.Create()
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The current migration SQL generator 'CustomSqlServerMigrationsSqlGenerator' is unable to generate SQL for operations of type 'Microsoft.EntityFrameworkCore.Migrations.Operations.MySqlCreateDatabaseOperation'.
Today, with Net 5.0 RC2 and Pomelo.EntityFrameworkCore.MySql:
public partial class BaseEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("ID", Order = 0)]
public long Id { get; set; }
[Column("CREATED_AT", Order = 995)]
[IgnoreDataMember]
[JsonIgnore]
public DateTime? CreatedAt { get; set; }
[Column("CREATED_BY_SESSION_ID", Order = 996)]
[IgnoreDataMember]
[JsonIgnore]
public long CreatedBySessionId { get; set; }
[Column("UPDATED_AT", Order = 997)]
[IgnoreDataMember]
[JsonIgnore]
public DateTime? UpdatedAt { get; set; }
[Column("UPDATED_BY_SESSION_ID", Order = 998)]
[IgnoreDataMember]
[JsonIgnore]
public long UpdatedBySessionId { get; set; }
[Column("VERSION_ID", Order = 999)]
public long? VersionId { get; set; }
}
[Table("Tb_Geo_Zone")]
public class GeoZona :BaseEntity
{
[Required]
[Column("codice", Order = 1)]
[StringLength(10)]
public string codice { get; set; }
[Column("nome", Order = 2)]
[StringLength(10)]
public string Nome { get; set; }
}
Sql autogenerated from Migration:
CREATE TABLE Tb_Geo_Zone ( ID bigint NOT NULL AUTO_INCREMENT, codice varchar(10) CHARACTER SET utf8mb4 NOT NULL, nome varchar(10) CHARACTER SET utf8mb4 NULL, CREATED_AT datetime(6) NULL, CREATED_BY_SESSION_ID bigint NOT NULL, UPDATED_AT datetime(6) NULL, UPDATED_BY_SESSION_ID bigint NOT NULL, VERSION_ID bigint NULL,
CONSTRAINT PK_Tb_Geo_Zone PRIMARY KEY (ID),
CONSTRAINT AK_Tb_Geo_Zone_codice UNIQUE (codice)
);
Activity
[-]Column order broken in migration while class inheriting few column sfrom another class[/-][+]Column order broken in migration while model class inheriting few columns from another class[/+]ajcvickers commentedon Mar 19, 2018
@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?
tanvirarjel commentedon Mar 19, 2018
@ajcvickers Yes! Primary key should be the first column and the unmapped based type columns should go at the end..
ajcvickers commentedon Mar 19, 2018
@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.)
tanvirarjel commentedon Mar 20, 2018
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?
ajcvickers commentedon Mar 20, 2018
@tanvirarjel Thanks for the additional information. It will help us decide our course of action here.
ajcvickers commentedon Mar 22, 2018
Triage decisions:
tanvirarjel commentedon Mar 22, 2018
Dear @ajcvickers, Thanks a lot! for taking speedy action..That's why we love and rely on Microsoft :)
85 remaining items
bricelam commentedon Aug 10, 2020
Nobody here has any expectations about unmapped types in the middle of a hierarchy, right? This is just about the case identified in the issue description?
bricelam commentedon Aug 10, 2020
Also, if an owned type has an unmapped base type, it's properties should go next to the other owned type's properties still, right?
Migrations: Order unmapped base class properties last
tanvirarjel commentedon Aug 11, 2020
Yes! but if anybody wants this then he/should be able to use
ColumnOrderAttributeto specify the custom position,aalsamawi commentedon Sep 30, 2020
This doesn't work with MySQL. Any thoughts?
here's the error I'm getting:
pgiacomo69 commentedon Oct 16, 2020
Today, with Net 5.0 RC2 and Pomelo.EntityFrameworkCore.MySql:
public partial class BaseEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("ID", Order = 0)]
public long Id { get; set; }
[Column("CREATED_AT", Order = 995)]
[IgnoreDataMember]
[JsonIgnore]
public DateTime? CreatedAt { get; set; }
[Column("CREATED_BY_SESSION_ID", Order = 996)]
[IgnoreDataMember]
[JsonIgnore]
public long CreatedBySessionId { get; set; }
[Column("UPDATED_AT", Order = 997)]
[IgnoreDataMember]
[JsonIgnore]
public DateTime? UpdatedAt { get; set; }
[Column("UPDATED_BY_SESSION_ID", Order = 998)]
[IgnoreDataMember]
[JsonIgnore]
public long UpdatedBySessionId { get; set; }
[Column("VERSION_ID", Order = 999)]
public long? VersionId { get; set; }
}
[Table("Tb_Geo_Zone")]
public class GeoZona :BaseEntity
{
[Required]
[Column("codice", Order = 1)]
[StringLength(10)]
public string codice { get; set; }
Sql autogenerated from Migration:
CREATE TABLE
Tb_Geo_Zone(IDbigint NOT NULL AUTO_INCREMENT,codicevarchar(10) CHARACTER SET utf8mb4 NOT NULL,nomevarchar(10) CHARACTER SET utf8mb4 NULL,CREATED_ATdatetime(6) NULL,CREATED_BY_SESSION_IDbigint NOT NULL,UPDATED_ATdatetime(6) NULL,UPDATED_BY_SESSION_IDbigint NOT NULL,VERSION_IDbigint NULL,CONSTRAINT
PK_Tb_Geo_ZonePRIMARY KEY (ID),CONSTRAINT
AK_Tb_Geo_Zone_codiceUNIQUE (codice));
Now it works!!!!
skini82 commentedon Mar 10, 2022
I'm having the same issue in EF Core 6.0.3 on VS2022 with SQLServer DB