DbContext.Database.Migrate() not providing password to the backend

My goal is to create seeds of users when the database is created.
I’m using idserver4, with npgsql, docker-compose.
The current behavior creates the database and as well the identityserver user manager tables (AspNetUsers, AspNetUserTokens, AspNetUserRoles, etc..). So I know it’s migrating that data to the database. But it skips over the Task of running the User seed because it throws a password exception:

Npgsql.NpgsqlException (0x80004005): No password has been provided but the backend requires one (in MD5)

Here’s the code in my Program.cs.

    public static void Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();

        using (var scope = host.Services.CreateScope())
        {
            var services = scope.ServiceProvider;
            try
            {
                var userManager = services.GetRequiredService<UserManager<User>>();
                var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
                var context = services.GetRequiredService<ApplicationDbContext>();

                context.Database.Migrate(); // ERROR HAPPENS HERE

                Task.Run(async () => await UserAndRoleSeeder.SeedUsersAndRoles(roleManager, userManager)).Wait(); // I NEED THIS TO RUN
            }
            catch (Exception ex)
            {
                var logger = services.GetRequiredService<ILogger<Program>>();
                logger.LogError(ex, "Error has occured while migrating to the database.");
            }
        }
        host.Run();
    }

Here is the code where it gets the connection string in Startup.cs:

services.AddDbContext<ApplicationDbContext>(options =>
{
  options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"),
  b =>
  {
    b.MigrationsAssembly("GLFManager.App");
  });
});

If I use a breakpoint here, it shows that the connection string was obtained along with the user id and password. I verified the password was correct. Or else I don’t think it would initially commit the Idserver user manager tables.

Here is my appsettings.json file where the connection string lives:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "Host=localhost;Port=33010;Database=glfdb;User Id=devdbuser;Password=devdbpassword"
  }
}

I’m thinking it’s somewhere in the docker-compose file where some configuration is not registering. This is the docker-compose file:

version: '3.4'

services:
  glfmanager.api:
    image: ${DOCKER_REGISTRY-}glfmanagerapi
    container_name: "glfmanager.api"
    build:
      context: .
      dockerfile: GLFManager.Api/Dockerfile
    ports:
      - "33000:80"
      - "33001:443"
    environment:
      - ConnectionStrings__DefaultConnection=Server=glfmanager.db;Database=glfdb;User Id=devdbuser:password=devdbpassword;
      - Identity_Authority=http://glfmanager.auth
    volumes:
      - .:/usr/src/app
    depends_on:
      - "glfmanager.db"

  glfmanager.auth:
    image: ${DOCKER_REGISTRY-}glfmanagerauth
    container_name: "glfmanager.auth"
    build:
      context: .
      dockerfile: GLFManager.Auth/Dockerfile
    ports:
      - "33005:80"
      - "33006:443"
    environment:
      - ConnectionStrings__DefaultConnection=Server=glfmanager.db;Database=glfdb;User Id=devdbuser:password=devdbpassword;
    volumes:
      - .:/usr/src/app
    depends_on:
      - "glfmanager.db"

  glfmanager.db:
    restart: on-failure
    image: "mdillon/postgis:11"
    container_name: "glfmanager.db"
    environment:
      - POSTGRES_USER=devdbuser
      - POSTGRES_DB=glfdb
      - POSTGRES_PASSWORD=devdbpassword
    volumes:
      - glfmanager-db:/var/lib/postresql/data
    ports:
      - "33010:5432"


volumes:
  glfmanager-db:

I used this code from a class I took on backend developing and the code is Identitcal to the project I’ve built in that, and it works. So I’m stumped as to why this is giving me that password error.

Source: Docker Questions

LEAVE A COMMENT