EF Core Postgres Update-Database is trying to Create Database which already exists

1

I am using PostgreSQL on EF Core in .NET Core 3.1. On my local machine, I was able to do Update-Database successfully, which created the database and applied migrations.

Now we are moving to a development environment, and our DBAs created the database already, I just need to be able to run the migrations to set up the tables.

However, When I run Update-Database, I get the error:

CREATE DATABASE "Zeal";
Npgsql.PostgresException (0x80004005): 42501: permission denied to create database

I don't know why it's trying to create the database if it already exists in the first place. The migrations that I created do not state anything about actually creating the database. How can I force it to not try to run the CREATE DATABASE Zeal part of this script? Everything after that should be correct.

I tried removing all my migrations and the ZealDbContextModelSnapshot, running Add-Migration Initial, and then Update-Database to try to reset it, but I got the same result.

Here is my ContextFactory code. I presume there is some option I can set here to disable database creation step during migrations, but I can't find any solution online.

    public class ZealDbContextFactory : IDesignTimeDbContextFactory<ZealDbContext>
    {
        public IConfigurationRoot _config;
        public string _connString = "";

        public ZealDbContextFactory()
        {
            var env = "DEV";

            _config = new ConfigurationBuilder()
                .AddJsonFile("dbsettings.json")
                .Build();
            _connString = _config.GetConnectionString("ZealConnection_" + env);
        }

        public ZealDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ZealDbContext>();
            optionsBuilder.UseNpgsql(_connString, x =>
            {
                //what options can I set here to disable database creation step during Update-Database?
            });
            return new ZealDbContext(optionsBuilder.Options, new MockConfiguration { EnvironmentName = "DEV" }, null!);
        }
c#
postgresql
entity-framework-core
ef-core-3.0
asked on Stack Overflow Nov 6, 2020 by DLeh

1 Answer

1

Apparently database names in Postgres are case sensitive. I requested my DBAs to to create the database Zeal but got zeal. When I updated my connection string to zeal, the migrations applied successfully.

answered on Stack Overflow Nov 6, 2020 by DLeh • edited Nov 13, 2020 by DLeh

User contributions licensed under CC BY-SA 3.0