Open Closed

DbMigrator throws exception on initial database creation #4417


User avatar
0
jlavallet created
  • ABP Framework version: v 5.3.5
  • UI type: MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:

 D:\Century\Internal\BlueSpot\BlueSpotWorking\src\BlueSpot.DbMigrator\bin\Release\net6.0\publish master ≡  ?6 ~10 -30 ➜ .\BlueSpot.DbMigrator.exe [15:26:47 INF] Started database migrations... [15:26:47 INF] Migrating schema for host database... [15:26:51 ERR] Failed executing DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE [AbpAuditLogs] ( [Id] uniqueidentifier NOT NULL, [ApplicationName] nvarchar(96) NULL, [UserId] uniqueidentifier NULL, [UserName] nvarchar(256) NULL, [TenantId] uniqueidentifier NULL, [TenantName] nvarchar(64) NULL, [ImpersonatorUserId] uniqueidentifier NULL, [ImpersonatorUserName] nvarchar(256) NULL, [ImpersonatorTenantId] uniqueidentifier NULL, [ImpersonatorTenantName] nvarchar(64) NULL, [ExecutionTime] datetime2 NOT NULL, [ExecutionDuration] int NOT NULL, [ClientIpAddress] nvarchar(64) NULL, [ClientName] nvarchar(128) NULL, [ClientId] nvarchar(64) NULL, [CorrelationId] nvarchar(64) NULL, [BrowserInfo] nvarchar(512) NULL, [HttpMethod] nvarchar(16) NULL, [Url] nvarchar(256) NULL, [Exceptions] nvarchar(max) NULL, [Comments] nvarchar(256) NULL, [HttpStatusCode] int NULL, [ExtraProperties] nvarchar(max) NULL, [ConcurrencyStamp] nvarchar(40) NULL, CONSTRAINT [PK_AbpAuditLogs] PRIMARY KEY ([Id]) ); Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'AbpAuditLogs' in the database. at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 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.InternalEndExecuteNonQuery(IAsyncResult asyncResult, Boolean isInternal, String endMethod) at Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult) at Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryAsync(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization) --- End of stack trace from previous location --- at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQueryAsync(IEnumerable1 migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQueryAsync(IEnumerable1 migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQueryAsync(IEnumerable1 migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQueryAsync(IEnumerable1 migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken) at BlueSpot.EntityFrameworkCore.EntityFrameworkCoreBlueSpotDbSchemaMigrator.MigrateAsync() in D:\Century\Internal\BlueSpot\BlueSpotWorking\src\BlueSpot.EntityFrameworkCore\EntityFrameworkCore\EntityFrameworkCoreBlueSpotDbSchemaMigrator.cs:line 40 at BlueSpot.Data.BlueSpotDbMigrationService.MigrateDatabaseSchemaAsync(Tenant tenant) in D:\Century\Internal\BlueSpot\BlueSpotWorking\src\BlueSpot.Domain\Data\BlueSpotDbMigrationService.cs:line 103 at BlueSpot.Data.BlueSpotDbMigrationService.MigrateAsync() in D:\Century\Internal\BlueSpot\BlueSpotWorking\src\BlueSpot.Domain\Data\BlueSpotDbMigrationService.cs:line 60 at BlueSpot.DbMigrator.DbMigratorHostedService.StartAsync(CancellationToken cancellationToken) in D:\Century\Internal\BlueSpot\BlueSpotWorking\src\BlueSpot.DbMigrator\DbMigratorHostedService.cs:line 41 at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) at BlueSpot.DbMigrator.Program.Main(String[] args) in D:\Century\Internal\BlueSpot\BlueSpotWorking\src\BlueSpot.DbMigrator\Program.cs:line 35 at BlueSpot.DbMigrator.Program.<Main>(String[] args) ClientConnectionId:df8ce6da-e222-4fc1-a5d7-9c800aad5d1f Error Number:2714,State:6,Class:16  15:26:55  ﮫ 45.824s   D:\Century\Internal\BlueSpot\BlueSpotWorking\src\BlueSpot.DbMigrator\bin\Release\net6.0\publish master ≡  ?6 ~10 -30 ➜

  • Steps to reproduce the issue:"

I decided start afresh with a new database and as I am still in the early phases of development I decided to reset my migrations.

  1. I first deleted my Local DB database.
  2. Then I deleted the Migrations folder and the TenantMigrations folder in my EntityFrameworkCore project.
  3. After deleting these folders, I ran the DbMigrator using Visual Studio by right clicking on the BlueSpot.DbMigrator project and selecting Debug > Start New Instance. The migration started and ran for a bit and then generated the error above.
  4. I decided to try it from the command line, so I published to a folder and then navigated into that folder to run the BlueSpot.DbMigrator.exe. I got the same error message.

My intention is to use a shared host/tenant database and my suspicions are that the tenant database initial migration is also being run. Both initial migrations have code to create the ABP tables. The first table and each migration is AbpAuditLogs. It sure looks like it's trying to do the table creation a second time without regard to the existing table.

Why is this happening, and what can I do about it?


6 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Then I deleted the Migrations folder and the TenantMigrations folder in my EntityFrameworkCore project.

    Do you add ef core migrations again?

  • User Avatar
    0
    jlavallet created

    I reran the DbMigrator and the folders were re-created. Do I need to do something else?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Please check the connection string again, You can debug the app to see the connection string.

  • User Avatar
    0
    jlavallet created

    I am away from my workstation at the moment. I am trying to do this remotely. Where do I set the breakpoint to check the connection string? I do have an appsettings.Production.json but it should not be picking that up.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can just get value from configuration[ConnectionStrings:Default]

  • User Avatar
    0
    jlavallet created

    I think it was picking up my production connection string instead of my local development connection string. When I excluded the appsettings.Production.json file from the project everything worked fine.

    Thanks for your help!

Made with ❤️ on ABP v9.1.0-preview. Updated on December 10, 2024, 06:38