Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • This is the first time the migrations run.
  • The database already exists (it's actually an Umbraco database).

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!

like image 414
Floris Robbemont Avatar asked Jan 11 '13 11:01

Floris Robbemont


People also ask

Is it possible to create a code first model based on the existing database?

To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item.. Select ADO.NET Entity Data Model in the Add New Item dialog box and specify the model name (this will be a context class name) and click on Add. This will open the Entity Data Model wizard as shown below.

How do I get rid of migrations in Entity Framework?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

What is the use of migration in MVC?

The Migrations feature enables you to change the data model and deploy your changes to production by updating the database schema without having to drop and re-create the database.


1 Answers

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

like image 164
Marty Avatar answered Oct 19 '22 10:10

Marty