How do I migrate asp.net 2.2 identity tables to asp.net 3.1 schema?

0

I started a project with asp.net core 2.2. I developed the database using the "code first" approach. Later, I created a new solution with asp.net 3.1. I generated an initial migration from that existing database. Lastly, I added authentication and roles to the new asp.net 3.1 web app. Unfortunately, now it seems that EF Core wants to make changes to the identity tables and it is failing. The error message is:

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
  Failed executing DbCommand (17ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
  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'[AspNetUserTokens]') AND [c].[name] = N'Name');
  IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [AspNetUserTokens] DROP CONSTRAINT [' + @var0 + '];');
  ALTER TABLE [AspNetUserTokens] ALTER COLUMN [Name] nvarchar(128) NOT NULL;
Failed executing DbCommand (17ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
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'[AspNetUserTokens]') AND [c].[name] = N'Name');

IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [AspNetUserTokens] DROP CONSTRAINT [' + @var0 + '];');
ALTER TABLE [AspNetUserTokens] ALTER COLUMN [Name] nvarchar(128) NOT NULL;
Microsoft.Data.SqlClient.SqlException (0x80131904): The object 'PK_AspNetUserTokens' is dependent on column 'Name'.
ALTER TABLE ALTER COLUMN Name failed because one or more objects access this column.
   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:bd780164-061d-4b48-bdf0-ae872d83dfd1
Error Number:5074,State:1,Class:16
The object 'PK_AspNetUserTokens' is dependent on column 'Name'.
ALTER TABLE ALTER COLUMN Name failed because one or more objects access this column.

The migration code that it wants to run is:

    migrationBuilder.AlterColumn<string>(
        name: "Name",
        table: "AspNetUserTokens",
        maxLength: 128,
        nullable: false,
        oldClrType: typeof(string),
        oldType: "nvarchar(450)");

    migrationBuilder.AlterColumn<string>(
        name: "LoginProvider",
        table: "AspNetUserTokens",
        maxLength: 128,
        nullable: false,
        oldClrType: typeof(string),
        oldType: "nvarchar(450)");

    migrationBuilder.AlterColumn<string>(
        name: "ProviderKey",
        table: "AspNetUserLogins",
        maxLength: 128,
        nullable: false,
        oldClrType: typeof(string),
        oldType: "nvarchar(450)");

    migrationBuilder.AlterColumn<string>(
        name: "LoginProvider",
        table: "AspNetUserLogins",
        maxLength: 128,
        nullable: false,
        oldClrType: typeof(string),
        oldType: "nvarchar(450)");

My guess is, there were some changes made to this between asp.net 2.2 and asp.net 3.1. But now, how do I reconcile this?

asp.net-core
entity-framework-core
asp.net-identity-3
ef-core-3.0
asked on Stack Overflow Jan 8, 2020 by tnk479

1 Answer

0

I suppose this is an application in your local machine, so why don't you just drop (delete) the database? Entity Framework will re-create it when you run the application again. If you want to update your database on a SQL Server instance you can make use of SQL Server Data tools and perform a schema compare.

answered on Stack Overflow Jan 8, 2020 by Giorgos Manoltzas

User contributions licensed under CC BY-SA 3.0