Devart.Data.PostgreSql.Entity.EFCore not marking int keys as serial when generating migration script
Posted: Wed 13 Nov 2019 17:26
Hey, I'm having to make some customizations on EntityFrameworkCore to allow the generation, and execution of migrations scripts with dynamic schema.
The central idea is that migrations will be created at development time without schema, than some time latter will be executed several times with different schema, that will be set as the default schema for model of the DbContext at runtime.
To allow that, I had to replace the IMigrationsAssembly service with a custom one. My custom implementation just inheret from the default MigrationsAssembly class and override the values of the properties Schema and PrincipalSchema for all MigrationOperations from a given DbContext.
This works for the SqlServer provider, and the Npgsql provider for PostgreSQL. But I'm facing a issue with your EFCore provider.
When I have an entity with a int key, the script generated does not mark the key column as serial type.
What I was able to find, was that the CreateTableOperation has a Columns property that is a list os AddColumnOperation. This type also has a Schema property. When the Schema property of the AddColumnOperation that is resposible for the key column is set or overriden, the Create Table statement in the generated script, will not mark the key column as serial.
This Github repository has this scenario. https://github.com/jamir-araujo/DevartCustomMigration
To reproduce, open the solution, go to the Package Manager Console, select the EntityFramework.Devart.PostgreSql project, and execute the command Script-Migration -from 0 -to CreateDatabase.
This will generate a migration script with the issue described.
Now, go to the ForceSchemaMigrationsAssembly class that is on the EntityFramework.Devart.PostgreSql projetct, uncomment the line if (addColumnOperation.Name != "Id") and execute the previous Script-Migration command again.
This time the migration script is correct.
The central idea is that migrations will be created at development time without schema, than some time latter will be executed several times with different schema, that will be set as the default schema for model of the DbContext at runtime.
To allow that, I had to replace the IMigrationsAssembly service with a custom one. My custom implementation just inheret from the default MigrationsAssembly class and override the values of the properties Schema and PrincipalSchema for all MigrationOperations from a given DbContext.
This works for the SqlServer provider, and the Npgsql provider for PostgreSQL. But I'm facing a issue with your EFCore provider.
When I have an entity with a int key, the script generated does not mark the key column as serial type.
What I was able to find, was that the CreateTableOperation has a Columns property that is a list os AddColumnOperation. This type also has a Schema property. When the Schema property of the AddColumnOperation that is resposible for the key column is set or overriden, the Create Table statement in the generated script, will not mark the key column as serial.
This Github repository has this scenario. https://github.com/jamir-araujo/DevartCustomMigration
To reproduce, open the solution, go to the Package Manager Console, select the EntityFramework.Devart.PostgreSql project, and execute the command Script-Migration -from 0 -to CreateDatabase.
This will generate a migration script with the issue described.
Now, go to the ForceSchemaMigrationsAssembly class that is on the EntityFramework.Devart.PostgreSql projetct, uncomment the line if (addColumnOperation.Name != "Id") and execute the previous Script-Migration command again.
This time the migration script is correct.