Database already exists when running EF Core migrations
Posted by Dylan Beattie on 01 February 2022 • permalinkI’m working on a project that uses Entity Framework Core, and I’m using EF Core Migrations to manage database state. Earlier today, I grabbed a fresh (obfuscated) snapshot of the production database, copied it across to my workstation, and tried to run dotnet ef database update
to apply the latest migrations from my current branch:
[14:37:27 INF] Entity Framework Core 6.0.1 initialized 'MyDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.1' with options: using lazy loading proxies
[14:37:27 ERR] Failed executing DbCommand (15ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
CREATE DATABASE [my-database]
COLLATE SQL_Latin1_General_CP1_CI_AI;
Failed executing DbCommand (15ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
CREATE DATABASE [my-database]
COLLATE SQL_Latin1_General_CP1_CI_AI;
Microsoft.Data.SqlClient.SqlException (0x80131904): Database 'my-database' already exists. Choose a different database name.
It’s failing because the database exists… but I know the database exists – I just created it! It shouldn’t be trying to create a new database at all… right?
It turns out that the database exists, but the user account doesn’t. My application’s appSettings.development.json
contains a database connection string which specifies a username and password:
{
"ConnectionStrings": {
"MyDatabase": "Server=localhost;Database=my-database;User Id=my-user;Password=p@ssw0rd;MultipleActiveResultSets=true;Connect Timeout=1"
}
}
That username/password is still a valid server login on localhost (and, hey, look at that super-secret password right there!), but there’s no corresponding user in the my-database
database, so I’m guessing what happens here is EF Core connects to the server (which works), tries to open the database, can’t open it, and so assumes it doesn’t exist – and so attempts to create it, which fails because it already exists.
I recreated the database user linked to the login (and added them to the db_owner
role so that EF Core has permission to create tables, indexes, etc.):
USE [my-database]
GO
CREATE USER [my-user] FOR LOGIN [my-user] WITH DEFAULT_SCHEMA=[dbo]
GO
USE [my-database]
GO
ALTER ROLE [db_owner] ADD MEMBER [my-user]
GO
and it worked just fine.