Floris Robbemont
Floris Robbemont

Reputation: 163

EntityFramework migrations tries to create an existing database

We're deploying a simple ASP.NET MVC application to on of our staging servers and we're getting the following error when EntityFramework tries to migrate the existing database.

CREATE DATABASE permission denied in database 'master'. 

The situation is as follows:

It would appear the the following method (from EntityFramework) returns false, when it obviously should return true:

System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists() 

The database initializer has the following code:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, Configuration>());

Automatic migrations is turned off, because we're using Code-Based migrations.

The connectionstring points to the correct database and the user has db_owner rights on that database. The user has no rights to other tables on the server.

Is this a common problem? Does anyone know a solution for this?

Thnx!

Upvotes: 12

Views: 3298

Answers (1)

Marty
Marty

Reputation: 3555

Ensure database exists - executes the following script

IF db_id(N'MyDatabaseName') IS NOT NULL SELECT 1 ELSE SELECT Count(*) FROM sys.databases WHERE [name]=N'MyDatabaseName'

that assumes users permission to query 'sys.databases'for a list of DB's. If this query fails - EF thinks that DB has NOT been created and tries to create it again.

Make sure - that the user executing the migration, is able to execute the query above successfully

Upvotes: 4

Related Questions