.net core EF核心代码首次迁移将主键从Int更改为Long失败

.net core EF核心代码首次迁移将主键从Int更改为Long失败,.net-core,entity-framework-core,azure-sql-database,.net Core,Entity Framework Core,Azure Sql Database,我正在使用Dotnet Core 3.1和Entity Framework Core 3.1.3,首先在Azure SQL数据库中使用代码 在开发过程中,我希望将表的主键从dotnet核心项目中的Model类从int改为long,并使用添加迁移。但是,使用更新数据库时,会出现以下错误: Failed executing DbCommand (153ms) [Parameters=[], CommandType='Text', CommandTimeout='600'] DECLARE @var0

我正在使用Dotnet Core 3.1和Entity Framework Core 3.1.3,首先在Azure SQL数据库中使用代码

在开发过程中,我希望将表的主键从dotnet核心项目中的Model类从int改为long,并使用添加迁移。但是,使用更新数据库时,会出现以下错误:

Failed executing DbCommand (153ms) [Parameters=[], CommandType='Text', CommandTimeout='600']
DECLARE @var0 sysname;
SELECT @var0 = [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'[Warnings]') AND [c].[name] = N'Id');
IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [Warnings] DROP CONSTRAINT [' + @var0 + '];');
ALTER TABLE [Warnings] ALTER COLUMN [Id] bigint NOT NULL;
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (153ms) [Parameters=[], CommandType='Text', CommandTimeout='600']
      DECLARE @var0 sysname;
      SELECT @var0 = [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'[Warnings]') AND [c].[name] = N'Id');
      IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [Warnings] DROP CONSTRAINT [' + @var0 + '];');
      ALTER TABLE [Warnings] ALTER COLUMN [Id] bigint NOT NULL;
Microsoft.Data.SqlClient.SqlException (0x80131904): ALTER TABLE failed because the following SET options have incorrect settings: 'ANSI_WARNINGS'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations.
   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(TaskCompletionSource`1 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.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)
ClientConnectionId:774960a2-b0a7-4134-8050-0b8b18be8d37
Error Number:1934,State:1,Class:16
ALTER TABLE failed because the following SET options have incorrect settings: 'ANSI_WARNINGS'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations.
执行DbCommand失败(153ms)[参数=[],CommandType='Text',CommandTimeout='600']
声明@var0 sysname;
选择@var0=[d]。[name]
来自[sys].[default_constraints][d]
[d].[parent\u column\u id]=[c].[column\u id]和[d].[parent\u object\u id]=[c].[object\u id]上的内部联接[sys].[columns][c]
其中([d].[parent_object_id]=object_id(N'[Warnings]')和[c].[name]=N'id');
如果@var0不是NULL EXEC(N'ALTER TABLE[Warnings]DROP CONSTRAINT['+@var0+'];');
ALTER TABLE[Warnings]ALTER COLUMN[Id]bigint NOT NULL;
失败:Microsoft.EntityFrameworkCore.Database.Command[20102]
执行DbCommand失败(153ms)[Parameters=[],CommandType='Text',CommandTimeout='600']
声明@var0 sysname;
选择@var0=[d]。[name]
来自[sys].[default_constraints][d]
[d].[parent\u column\u id]=[c].[column\u id]和[d].[parent\u object\u id]=[c].[object\u id]上的内部联接[sys].[columns][c]
其中([d].[parent_object_id]=object_id(N'[Warnings]')和[c].[name]=N'id');
如果@var0不是NULL EXEC(N'ALTER TABLE[Warnings]DROP CONSTRAINT['+@var0+'];');
ALTER TABLE[Warnings]ALTER COLUMN[Id]bigint NOT NULL;
Microsoft.Data.SqlClient.SqlException(0x80131904):ALTER TABLE失败,因为以下集合选项的设置不正确:“ANSI_警告”。验证集合选项是否正确用于索引视图和/或计算列上的索引和/或筛选索引和/或查询通知和/或XML数据类型方法和/或空间索引操作。
位于Microsoft.Data.SqlClient.SqlConnection.OneError(SqlException异常、布尔断开连接、操作'1 wrapCloseInAction)
位于Microsoft.Data.SqlClient.SqlInternalConnection.OneError(SqlException异常、布尔断开连接、操作'1 wrapCloseInAction)
位于Microsoft.Data.SqlClient.TdsParser.throweexception和warning(TdsParserStateObject stateObj、布尔调用者连接锁、布尔异步关闭)
位于Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior RunBehavior、SqlCommand cmdHandler、SqlDataReader dataStream、BulkCopySimpleResultSet bulkCopyHandler、TdsParserStateObject stateObj、Boolean和dataReady)
位于Microsoft.Data.SqlClient.SqlCommand.RunExecuteOnQueryTds(字符串方法名、布尔isAsync、Int32超时、布尔asyncWrite)
位于Microsoft.Data.SqlClient.SqlCommand.InternalExecuteOnQuery(TaskCompletionSource`1完成,布尔sendToPipe,Int32超时,布尔和usedCache,布尔异步写入,布尔inRetry,字符串方法名)
在Microsoft.Data.SqlClient.SqlCommand.ExecuteOnQuery()中
位于Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteOnQuery(RelationalCommandParameterObject parameterObject)
位于Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteOnQuery(IRelationalConnection连接,iRelationalYDictionary`2参数值)
位于Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecutionQuery(IEnumerable`1 migrationCommands,IRelationalConnection)
位于Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(字符串targetMigration)
位于Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration,String contextType)
位于Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(字符串targetMigration,字符串contextType)
在Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.c__DisplayClass0_0.b_0()中
位于Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(操作)
客户连接ID:774960a2-b0a7-4134-8050-0b8b18be8d37
错误编号:1934,州:1,类别:16
ALTER TABLE失败,因为以下设置选项的设置不正确:“ANSI_警告”。验证集合选项是否正确用于索引视图和/或计算列上的索引和/或筛选索引和/或查询通知和/或XML数据类型方法和/或空间索引操作。

如何解决此问题?

可能您有使用此id的外键。如果有,您不能这样做,您应该先删除外键,然后重试,然后再次添加外键

您可能有使用此id的外键。如果有,您不能这样做,您应该先删除外键,然后重试,然后再次添加外键

我也在尝试做同样的事情:更改主键的数据类型。我一直在删除并重新创建所有外键和主键,但仍然出现错误

ALTER TABLE失败,因为以下集合选项的设置不正确:“ANSI_警告”

最后,我用一个
alterindex-ALL-ON-Warnings DISABLE;
和一个
alterindex-ALL-ON-Warnings-REBUILD;
解决了这个问题。因此,我的最后一个脚本如下所示:

//删除所有fk
migrationBuilder.DropForeignKey(“FK_警告消息_警告ID”、“消息”);
//下降峰
migrationBuilder.DropPrimaryKey(“PK_警告”、“警告”);
//禁用所有索引
Sql(“ALTER INDEX ALL ON Warnings DISABLE;”;
//更新数据类型
migrationBuilder.AlterColumn(名称:“Id”,
表:“警告”,
类型:“长”,
可为空:false,
oldClrType:typeof(int),
旧式:“int”)
.Annotation(“SqlServer:Identity”、“1,1”)
.OldAnnotation(“SqlServer:Identity”,“1,1”)
migrationBuilder.AlterColumn(
名称:“警告ID”,
表:“信息”,
类型:“长”,
可为空:false,
oldClrType:typeof(int),
旧式:“int”);
//可重入索引
惯性矩