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!
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With