Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't generate migration which does seeding with TPT and identity/sequence generation #2501

Closed
antoineatstariongroup opened this issue Sep 8, 2022 · 3 comments · Fixed by #2502
Assignees
Labels
bug Something isn't working
Milestone

Comments

@antoineatstariongroup
Copy link

antoineatstariongroup commented Sep 8, 2022

A migration provides the following generated method :

 protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.UpdateData(
                table: "AspNetRoles",
                keyColumn: "Id",
                keyValue: "AF8956F8-CA85-4DF2-8CB6-C46D0845B987",
                column: "ConcurrencyStamp",
                value: "e718543c-b540-465c-99da-b12c1276826b");

            migrationBuilder.UpdateData(
                table: "AspNetUsers",
                keyColumn: "Id",
                keyValue: "F3E3BACF-5F7C-4657-88E9-FA904EFB64D7",
                columns: new[] { "ConcurrencyStamp", "PasswordHash", "SecurityStamp" },
                values: new object[] { "dc0b5b80-6508-4638-a5ff-60cbb0f5f342", "AQAAAAEAACcQAAAAEIRpol47PPunZ80z2nNoHH04oontoJEqfpkJFVDBcv8D0Hrm78+uJh/w4+3AFGfPSg==", "b6c8f81a-bf91-4edb-9cff-71c45a61a94d" });

            migrationBuilder.InsertData(
                table: "Entity",
                column: "Id",
                values: new object[]
                {
                    new Guid("939b75ce-c3b4-4b94-9382-27cd4c5093fa"),
                    new Guid("ed7bed81-207a-436a-9798-24b9f1a79e34")
                });

            migrationBuilder.InsertData(
                table: "Project",
                columns: new[] { "Id", "EntityContainerId", "ProjectName" },
                values: new object[] { new Guid("ed7bed81-207a-436a-9798-24b9f1a79e34"), null, "Test Project" });

            migrationBuilder.InsertData(
                table: "Participant",
                columns: new[] { "Id", "EntityContainerId", "RoleId", "UserId" },
                values: new object[] { new Guid("939b75ce-c3b4-4b94-9382-27cd4c5093fa"), new Guid("ed7bed81-207a-436a-9798-24b9f1a79e34"), new Guid("fd580a55-9666-4abe-a02b-3a99478996f7"), new Guid("3503bf4c-1211-41eb-b369-aaa6bbdf5ff8") });
        }

When trying to update the database, this following error appears:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlSqlGenerationHelper.RequiresQuoting(String identifier)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlSqlGenerationHelper.DelimitIdentifier(String identifier)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.NpgsqlMigrationsSqlGenerator.<>c__DisplayClass4_0.<Generate>g__AddSequenceBumpingForSeeding|0()
   at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.NpgsqlMigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.<>c__DisplayClass16_2.<GetMigrationCommandLists>b__2()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.

If we replace the generated

 migrationBuilder.InsertData(
                table: "Entity",
                column: "Id",
                values: new object[]
                {
                    new Guid("939b75ce-c3b4-4b94-9382-27cd4c5093fa"),
                    new Guid("ed7bed81-207a-436a-9798-24b9f1a79e34")
                });

by

    migrationBuilder.Sql("INSERT INTO \"Entity\"(\"Id\") VALUES('{939b75ce-c3b4-4b94-9382-27cd4c5093fa}')");
    migrationBuilder.Sql("INSERT INTO \"Entity\"(\"Id\") VALUES('{ed7bed81-207a-436a-9798-24b9f1a79e34}')");

everything is working great.

@roji
Copy link
Member

roji commented Sep 8, 2022

@antoineatrhea are you able to submit a minimal project that shows this happening? A simple model for the above entity types, along with their seed data, should be sufficient.

@antoineatstariongroup
Copy link
Author

MigrationBug.zip
Here is a minimal project with the migration that fails.

@roji roji self-assigned this Sep 9, 2022
@roji roji added the bug Something isn't working label Sep 9, 2022
@roji roji added this to the 6.0.7 milestone Sep 9, 2022
@roji
Copy link
Member

roji commented Sep 9, 2022

Thanks, the missing piece was the TPT hierarchy - the post-seeding sequence bumping logic was going over the properties of all entities mapped to the table, and assuming that those properties are mapped to columns of the table; this included children properties which are in a separate table in TPT, and so aren't mapped to columns in the base table. Will fix.

roji added a commit to roji/efcore.pg that referenced this issue Sep 9, 2022
@roji roji changed the title Update-Database causes NullReferenceException Can't generate migration which does seeding with TPT and identity/sequence generation Sep 9, 2022
@roji roji closed this as completed in #2502 Sep 9, 2022
roji added a commit that referenced this issue Sep 9, 2022
roji added a commit that referenced this issue Sep 9, 2022
Fixes #2501

(cherry picked from commit 6cd895e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants