CREATE DATABASE permission denied in database 'master' (EF code-first)

I had the same problem. This what worked for me:

  1. Go to SQL Server Management Studio and run it as Administrator.
  2. Choose Security -> Then Logins
  3. Choose the usernames or whatever users that will access your database under the Logins and Double Click it.
  4. Give them a Server Roles that will give them credentials to create database. On my case, public was already checked so I checked dbcreator and sysadmin.
  5. Run update-database again on Package Manager Console. Database should now successfully created.

Here is an image so that you can get the bigger picture, I blurred my credentials of course:enter image description here


Be sure you have permission to create db.(as user2012810 mentioned.)

or

It seems that your code first use another (or default) connection string. Have you set connection name on your context class?

public class YourContext : DbContext
    {
        public YourContext() : base("name=DefaultConnection")
        {

        }

        public DbSet<aaaa> Aaaas { get; set; }
    }

I got the same problem when trying to create a database using Code First(without database approach). The problem is that EF doesn't have enough permissions to create a database for you.

So I worked my way up using the Code First(using an existing database approach).

Steps :

  1. Create a database in the Sql server management studio(preferably without tables).
  2. Now back on visual studio, add a connection of the newly created database in the server explorer.
  3. Now use the connection string of the database and add it in the app.config with a name like "Default Connection".
  4. Now in the Context class, create a constructor for it and extend it from base class and pass the name of the connection string as a parameter. Just like,

    public class DummyContext : DbContext
    {
      public DummyContext() : base("name=DefaultConnection")
      {
      }  
    }
    

5.And now run your code and see the tables getting added to the database provided.

Tags:

Code First