2

我将 Dotnet Core 3.1 与 Entity Framework Core 3.1.3 结合使用,并使用 Code-First 与 Azure SQL 数据库。

在开发过程中,我想从 dotnet core 项目中的 Model 类中将表的主键从 int 更改为 long 并使用add-migration。但是,在使用Update-Database时,会出现错误:

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.

如何解决这个问题?

4

1 回答 1

2

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

ALTER TABLE 失败,因为以下 SET 选项的设置不正确:“ANSI_WARNINGS”。

我最终解决了它与一个ALTER INDEX ALL ON Warnings DISABLE;配对ALTER INDEX ALL ON Warnings REBUILD;。所以我的最终脚本看起来像这样:

// drop all fk's
migrationBuilder.DropForeignKey("FK_Warnings_Messages_WarningId", "Messages");

// drop pk
migrationBuilder.DropPrimaryKey("PK_Warnings", "Warnings");

// disable all indexes
migrationBuilder.Sql("ALTER INDEX ALL ON Warnings DISABLE;");

// update data types
migrationBuilder.AlterColumn<long>(name: "Id",
        table: "Warnings",
        type: "long",
        nullable: false,
        oldClrType: typeof(int),
        oldType: "int")
    .Annotation("SqlServer:Identity", "1, 1")
    .OldAnnotation("SqlServer:Identity", "1, 1")

migrationBuilder.AlterColumn<long>(
        name: "WarningId",
        table: "Messages",
        type: "long",
        nullable: false,
        oldClrType: typeof(int),
        oldType: "int");

// reenable indexes
migrationBuilder.Sql("ALTER INDEX ALL ON Warnings REBUILD;");

// restore PK's
migrationBuilder.AddPrimaryKey("PK_Warnings", "Warnings", "Id");

// restore FK's
migrationBuilder.AddForeignKey("FK_Messages_Warnings_WarningId", "Messages", "WarningId", "Warnings", principalColumn: "Id", onDelete: ReferentialAction.Restrict);
于 2021-03-03T15:56:30.473 回答