abpframework / abp

Open-source web application framework for ASP.NET Core! Offers an opinionated architecture to build enterprise software solutions with best practices on top of the .NET. Provides the fundamental infrastructure, cross-cutting-concern implementations, startup templates, application modules, UI themes, tooling and documentation.
https://abp.io
GNU Lesser General Public License v3.0
12.88k stars 3.44k forks source link

ABP: Unable to connect in swagger + Missing admin user in DB #15939

Closed Enissay closed 1 year ago

Enissay commented 1 year ago

Documentation

Please check the official documentation before asking questions: https://docs.abp.io => Done

GitHub Issues

Project created using:

abp new MyProject \
 --template app \
 --ui blazor-server \
 --database-provider ef \
 --database-management-system PostgreSQL \
 --tiered \
 --create-solution-folder

Then, I update the ConnectionStrings AND the default password ClientSecret to use a longer one. The ConnectionStrings also has the extra SearchPath=dev since I am using a separate schema for different environment. Sample from DbMigrator project:

{
  "ConnectionStrings": {
    "Default": "Server=111.111.111.111;Port=5432;Database=my_db;User Id=my_user;Password=V7fiHngf9hcJbpqFVSD;SearchPath=dev;"
  },
  "OpenIddict": {
    "Applications": {
      "MyProject_Web": {
        "RootUrl": "https://localhost:44352"
        "ClientId": "MyProject_Web",
        "ClientSecret": "KX8MuY5N5rJvhOD7XZu3xUz4LcQmvf*"
      },
      "MyProject_App": {
        "RootUrl": "http://localhost:4200"
        "ClientId": "MyProject_App"
      },
      "MyProject_BlazorServerTiered": {
        "RootUrl": "https://localhost:44300"
        "ClientId": "MyProject_BlazorServerTiered",
        "ClientSecret": "KX8MuY5N5rJvhOD7XZu3xUz4LcQmvf*"
      },
      "MyProject_Swagger": {
        "RootUrl": "https://localhost:44200"
        "ClientId": "MyProject_Swagger"
      }
    }
  }
}

I have also changed the DB prefixes and schema as follows in 4 locations:

//// Change table prefixes & custom schema (hardcoded for test purpose only)
// src/MyProject.HttpApi.Host/Program.cs
// src/MyProject.AuthServer/Program.cs
// src/MyProject.DbMigrator/Program.cs
// src/MyProject.EntityFrameworkCore/EntityFrameworkCore/MyProjectDbContextFactory.cs

AbpIdentityDbProperties.DbTablePrefix = "";
AbpTenantManagementDbProperties.DbTablePrefix = "";
AbpAuditLoggingDbProperties.DbTablePrefix = "";
AbpCommonDbProperties.DbTablePrefix = "";
AbpPermissionManagementDbProperties.DbTablePrefix = "";
AbpSettingManagementDbProperties.DbTablePrefix = "";
AbpBackgroundJobsDbProperties.DbTablePrefix = "";
AbpFeatureManagementDbProperties.DbTablePrefix = "";
AbpOpenIddictDbProperties.DbTablePrefix = "";

AbpIdentityDbProperties.DbSchema = "dev";
AbpTenantManagementDbProperties.DbSchema = "dev";
AbpAuditLoggingDbProperties.DbSchema = "dev";
AbpCommonDbProperties.DbSchema = "dev";
AbpPermissionManagementDbProperties.DbSchema = "dev";
AbpSettingManagementDbProperties.DbSchema = "dev";
AbpBackgroundJobsDbProperties.DbSchema = "dev";
AbpFeatureManagementDbProperties.DbSchema = "dev";
AbpOpenIddictDbProperties.DbSchema = "dev";

Next I do generate the migrations and apply them. It all goes well with no errors:

dotnet ef migrations add \
    --startup-project src/MyProject.DbMigrator/MyProject.DbMigrator.csproj \
    --project src/MyProject.EntityFrameworkCore/MyProject.EntityFrameworkCore.csproj \
    --context MyProject.EntityFrameworkCore.MyProjectDbContext \
    --configuration Debug \
    --output-dir Migrations \
    Initial \
    -- --environment Development

dotnet ef database update \
    --startup-project src/MyProject.DbMigrator/MyProject.DbMigrator.csproj \
    --project src/MyProject.EntityFrameworkCore/MyProject.EntityFrameworkCore.csproj \
    --context MyProject.EntityFrameworkCore.MyProjectDbContext \
    --configuration Debug \
    --verbose \
    -- --environment Development

After launching the AuthServer, I was unable to connect as admin using the new psw KX8MuY5N5rJvhOD7XZu3xUz4LcQmvf*. I also noticed the table Users is completely empty.

I was also unable to connect into swagger as I get this: image

Any advice what I am missing ?

JadynWong commented 1 year ago

Did you run MyProject.DbMigrator project to initialize the data?

https://docs.abp.io/en/abp/latest/Getting-Started-Running-Solution?UI=BlazorServer&DB=EF&Tiered=Yes#running-the-dbmigrator

Enissay commented 1 year ago

Oh, I thought the dotnet ef migrations add was supposed to take care of it...

Anyhow, I did try many combinations in vain: raw dotnet run as suggested in documentation but it complained that the migrations were not found, they were actually in the src/MyProject.EntityFrameworkCore. I then added with many combinations of parameter --startup-project and --project but it all failed with different errors...

As a side note, the first error references a path like D:\ci\Jenkins\workspace\abp-framework-release\abp\... which does not even exist on my linux distrbution. I wonder if that is causing issues ?

## Try 1
~/dev/MyProject - 02:04:07 ❯  cd src/MyProject.DbMigrator/

~/dev/MyProject/src/MyProject.DbMigrator - 02:04:10 ❯ dotnet run
[02:04:17 INF] Creating initial migration...

~/dev/MyProject/src/MyProject.DbMigrator - 02:04:17 ❯ [02:04:18 INF] ABP CLI 7.0.3
[02:04:18 INF] Creating initial migrations...
[02:04:18 ERR] Creating initial migrations process is failed! Details:
No project was found. Change the current working directory or use the --project option.

[02:04:18 ERR] Migrations failed! A migration command didn't run successfully.
[02:04:18 ERR] Migrations failed! A migration command didn't run successfully.
System.Exception: Migrations failed! A migration command didn't run successfully.
   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 68
   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 168
   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 76

## Try 2 with params
~/dev/MyProject/src/MyProject.DbMigrator - 02:04:22 ❯ cd ../..

~/dev/MyProject - 02:04:28 ❯ dotnet run \
   --startup-project src/MyProject.EntityFrameworkCore/MyProject.EntityFrameworkCore.csproj \
   --project src/MyProject.DbMigrator/MyProject.DbMigrator.csproj \
   --context MyProject.EntityFrameworkCore.MyProjectDbContext \
   --configuration Debug \
   -- --environment Development

[02:04:37 INF] Started database migrations...
[02:04:37 INF] Migrating schema for host database...
Unhandled exception. System.ArgumentException: Host can't be null
   at Npgsql.NpgsqlConnectionStringBuilder.PostProcessAndValidate()
   at Npgsql.NpgsqlConnection.SetupDataSource()
   at Npgsql.NpgsqlConnection.set_ConnectionString(String value)
   at Npgsql.NpgsqlConnection..ctor(String connectionString)
   at Npgsql.NpgsqlConnection.CloneWith(String connectionString)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlRelationalConnection.CloneWith(String connectionString)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists(Boolean async, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.ExistsAsync(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
   at MyProject.EntityFrameworkCore.EntityFrameworkCoreMyProjectDbSchemaMigrator.MigrateAsync() in /home/enissay/dev/MyProject/src/MyProject.EntityFrameworkCore/EntityFrameworkCore/EntityFrameworkCoreMyProjectDbSchemaMigrator.cs:line 29
   at MyProject.Data.MyProjectDbMigrationService.MigrateDatabaseSchemaAsync(Tenant tenant) in /home/enissay/dev/MyProject/src/MyProject.Domain/Data/MyProjectDbMigrationService.cs:line 95
   at MyProject.Data.MyProjectDbMigrationService.MigrateAsync() in /home/enissay/dev/MyProject/src/MyProject.Domain/Data/MyProjectDbMigrationService.cs:line 52
   at MyProject.DbMigrator.DbMigratorHostedService.StartAsync(CancellationToken cancellationToken) in /home/enissay/dev/MyProject/src/MyProject.DbMigrator/DbMigratorHostedService.cs:line 36
   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 MyProject.DbMigrator.Program.Main(String[] args) in /home/enissay/dev/MyProject/src/MyProject.DbMigrator/Program.cs:line 60
   at MyProject.DbMigrator.Program.<Main>(String[] args)
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.