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

Exception when running migrations when deleting data and removing columns #22302

Closed
nathan-c opened this issue Aug 28, 2020 · 6 comments · Fixed by #22346
Closed

Exception when running migrations when deleting data and removing columns #22302

nathan-c opened this issue Aug 28, 2020 · 6 comments · Fixed by #22346
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Milestone

Comments

@nathan-c
Copy link

A set of migrations that work in 3.1.4 no longer work in 5.0.0-preview.8.20407.4. The problem seems to be when deleting data at the same time as dropping columns on an entity.

Steps to reproduce

EfCoreBug.zip
The attached zip contains a project with 2 migrations, one to create a table and insert data and another to change the table and reinsert data.

When running this using 5.0.0-preview.8.20407.4 I get the following exception.

System.InvalidOperationException: There is no property mapped to the column 'SomeEntity.Id' used in a data operation. Either add a property mapped to this column or specify the column types in the data operation.
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.GetMappedProperties(String[] names, String tableName, String schema, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.GenerateModificationCommands(DeleteDataOperation operation, IModel model)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(DeleteDataOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.<>c.<.cctor>b__79_29(MigrationsSqlGenerator g, MigrationOperation o, IModel m, MigrationCommandListBuilder b)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.<>c__DisplayClass16_2.<GetMigrationCommandLists>b__2()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
   at EfCoreBug.Program.Main(String[] args) in /home/nathan/git/EfCoreBug/EfCoreBug/Program.cs:line 15

Further technical details

EF Core version: 5.0.0-preview.8.20407.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer (also a problem using Npgsql.EntityFrameworkCore.Postgres)
Target framework: .NET Core 3.1
Operating system: Ubuntu 20.04
IDE: Rider 2020.2

@smitpatel
Copy link
Contributor

protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropPrimaryKey(
                name: "PK_SomeEntity",
                table: "SomeEntity");

            migrationBuilder.DeleteData(
                table: "SomeEntity",
                keyColumn: "Id",
                keyValue: -1L);

            migrationBuilder.DropColumn(
                name: "Id",
                table: "SomeEntity");

            migrationBuilder.AddColumn<string>(
                name: "Key",
                table: "SomeEntity",
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddPrimaryKey(
                name: "PK_SomeEntity",
                table: "SomeEntity",
                column: "Key");

            migrationBuilder.InsertData(
                table: "SomeEntity",
                columns: new[] { "Key", "Created", "Name", "Updated" },
                values: new object[] { "-1", new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "Hello", new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) });
        }

Issue here is that DeleteData operation is using the column name before rename and that is not found in the current model.
cc: @AndriySvyryd

@AndriySvyryd AndriySvyryd self-assigned this Aug 31, 2020
@AndriySvyryd AndriySvyryd added this to the 5.0.0 milestone Aug 31, 2020
AndriySvyryd added a commit that referenced this issue Sep 1, 2020
…oved column

Add fluent overloads to specify column types for Insert/Update/Delete operations

Fixes #21886
Fixes #22302
AndriySvyryd added a commit that referenced this issue Sep 1, 2020
…oved column

Add fluent overloads to specify column types for Insert/Update/Delete operations

Fixes #21886
Fixes #22302
@AndriySvyryd AndriySvyryd modified the milestones: 5.0.0, 5.0.0-rc1 Sep 1, 2020
@AndriySvyryd AndriySvyryd removed their assignment Sep 1, 2020
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 1, 2020
AndriySvyryd added a commit that referenced this issue Sep 1, 2020
…oved column

Add fluent overloads to specify column types for Insert/Update/Delete operations

Fixes #21886
Fixes #22302
@ajcvickers ajcvickers modified the milestones: 5.0.0-rc1, 5.0.0 Nov 7, 2020
@lomholdt
Copy link

For others finding this issue we solved it by adding a keyColumnType: "uniqueidentifier" on DeleteData in the migration that was failing.

@skkalahasti
Copy link

skkalahasti commented Dec 9, 2020

I had to specify the keycolumntype during deleteData to fix the exception when migrating to efcore 5.

@edukzs
Copy link

edukzs commented Mar 1, 2021

I had to specify the keycolumntype during deleteData to fix the exception when migrating to efcore 5.

this work for me

Same problem on 5.0.3 version

@nkolchakov
Copy link

Same problem on 7.0.3

@roji
Copy link
Member

roji commented Mar 15, 2023

@nkolchakov and others, can you please open a new issue with a minimal, runnable code sample?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants