I have an entity with the following:
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
[Key]
public int Id { get; set; }
which I changed to:
public int Id { get; set; }
When I created my migration and ran Update-Database
I got this error:
PM> Update-database Build started... Build succeeded. System.InvalidOperationException: To change the IDENTITY property of a column, the column needs to be dropped and recreated. at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(AlterColumnOperation operation, IModel model, MigrationCommandListBuilder builder) at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.<>c.<.cctor>b__71_4(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__DisplayClass15_2.b__2() at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.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) To change the IDENTITY property of a column, the column needs to be dropped and recreated.
The migration file contained:
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "Balance1",
nullable: false,
oldClrType: typeof(int),
oldType: "int")
.Annotation("SqlServer:Identity", "1, 1");
So I removed that and added:
migrationBuilder.DropColumn(
name: "Id",
table: "Balance1");
migrationBuilder.AddColumn<int>(
name: "Id",
table: "Balance1",
type: "int",
nullable: false)
.Annotation("SqlServer:Identity", "1, 1");
When I ran Update-Database
I got this error:
PM> Update-database Build started... Build succeeded. fail: Microsoft.EntityFrameworkCore.Database.Command[20102] Failed executing DbCommand (16ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] DECLARE @var1 sysname; SELECT @var1 = [d].[name] FROM [sys].[default_constraints] [d] INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id] WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Symptoms]') AND [c].[name] = N'CalibrationId'); IF @var1 IS NOT NULL EXEC(N'ALTER TABLE [Symptoms] DROP CONSTRAINT [' + @var1 + '];'); ALTER TABLE [Symptoms] ALTER COLUMN [CalibrationId] int NOT NULL; Failed executing DbCommand (16ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] DECLARE @var1 sysname; SELECT @var1 = [d].[name] FROM [sys].[default_constraints] [d] INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id] WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Symptoms]') AND [c].[name] = N'CalibrationId'); IF @var1 IS NOT NULL EXEC(N'ALTER TABLE [Symptoms] DROP CONSTRAINT [' + @var1 + '];'); ALTER TABLE [Symptoms] ALTER COLUMN [CalibrationId] int NOT NULL; Microsoft.Data.SqlClient.SqlException (0x80131904): Operand type clash: uniqueidentifier is incompatible with int at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction) at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction) at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite) at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName) at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary
2 parameterValues) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection) at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType) at Microsoft.EntityFrameworkCore.Design.O
How do I resolve this or is there an easy way to drop and recreate a column with Entity Framework Core?
User contributions licensed under CC BY-SA 3.0