Open Closed

More than one DBContext was found. #1662


User avatar
0
Spospisil created
  • ABP Framework version: v4.4.0
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes

When producing source using ABP Suite, I run the DbMigrator and get the following error

[15:37:12 INF] Creating initial migration... [15:37:13 INF] ABP CLI (https://abp.io) [15:37:13 INF] Version 4.4.0 (Stable) [15:37:19 ERR] Migrations failed! A migration command didn't run successfully:

Build started... Build succeeded. More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.

[15:37:19 ERR] Migrations failed! A migration command didn't run successfully:

Build started... Build succeeded. More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.

System.Exception: Migrations failed! A migration command didn't run successfully:

Build started... Build succeeded. More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.

at Volo.Abp.Cli.Commands.CreateMigrationAndRunMigratorCommand.ExecuteAsync(CommandLineArgs commandLineArgs) in D:\ci\Jenkins\workspace\abp-framework-release\abp\framework\src\Volo.Abp.Cli.Core\Volo\Abp\Cli\Commands\CreateMigrationAndRunMigratorCommand.cs:line 67 at Volo.Abp.Cli.CliService.RunInternalAsync(CommandLineArgs commandLineArgs) in D:\ci\Jenkins\workspace\abp-framework-release\abp\framework\src\Volo.Abp.Cli.Core\Volo\Abp\Cli\CliService.cs:line 158 at Volo.Abp.Cli.CliService.RunAsync(String[] args) in D:\ci\Jenkins\workspace\abp-framework-release\abp\framework\src\Volo.Abp.Cli.Core\Volo\Abp\Cli\CliService.cs:line 66

C:\Source Code\StructureWeb\src\StructureWeb.DbMigrator\bin\Debug\net5.0\StructureWeb.DbMigrator.exe (process 36876) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .


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

    hi

    You can manually create the migration according to the help document(src\StructureWeb.EntityFrameworkCore\README.md) of the project, and then execute DbMigrator .

    We will fix it. Thank you We have refunded your credit.


    How To Add New Migration?

    This solution configured so it uses two database schema;

    • Default schema is used to store host & tenant data (when the tenant uses shared database).
    • Tenant schema is used to store only the tenant data (when the tenant uses a dedicated database).

    In this way, dedicated tenant databases do not have host-related empty tables.

    To make this possible, there are two migrations DbContext in the EntityFrameworkCore project. So, you need to specify the DbContext when you want to add new migration.

    When you add/change a multi-tenant entity (that implements IMultiTenant) in your project, you typically need to add two migrations: one for the default DbContext and the other one is for the tenant DbContext. If you are making change for a host-only entity, then you don't need to add-migration for the tenant DbContext (if you add, you will get an empty migration file).

    Example: Adding Migration to the Default DbContext

    Using Visual Studio Package Manager Console;

    Add-Migration Your_Migration_Name -Context StructureWebDbContext
    

    Using EF Core command line tool;

    dotnet ef migrations add Your_Migration_Name --context StructureWebDbContext
    

    Example: Adding Migration to the Tenant DbContext

    Using Visual Studio Package Manager Console:

    Add-Migration Your_Migration_Name -Context StructureWebTenantDbContext -OutputDir TenantMigrations
    

    Using EF Core command line tool:

    dotnet ef migrations add Your_Migration_Name --context StructureWebTenantDbContext --output-dir TenantMigrations
    

    Updating the Databases

    It is suggested to run the DbMigrator application to update the database after adding a new migration. It is simpler and also automatically handles tenant database upgrades.

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