CREATE DATABASE permission denied in database 'master' (EF code-first)
I had the same problem. This what worked for me:
- Go to SQL Server Management Studio and run it as Administrator.
- Choose Security -> Then Logins
- Choose the usernames or whatever users that will access your database under the Logins and Double Click it.
- 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.
- 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:
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 :
- Create a database in the Sql server management studio(preferably without tables).
- Now back on visual studio, add a connection of the newly created database in the server explorer.
- Now use the connection string of the database and add it in the app.config with a name like "Default Connection".
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.