EF 4.1 CF: CREATE DATABASE permission denied in database 'master'
Solution 1
Did you make sure to set your Database Initializer to null
in your code:
Database.SetInitializer<MyDbContext>(null);
All built-in implementations if the initializer may try to drop or create a new database. The error you get indicate that EF tried to drop/create a database but hasn't the right to do so in your SQL Server instance. The line above is the only option to avoid this generally and is suited (I think even absolutely necessary) for live environments anyway where you don't want accidental deletion (due to model changes or something).
Solution 2
Setup a login for your application within SQL server. Give that user dbcreator permission (by right clicking on the login, go to server roles, and check the "dbcreator" checkbox)
Related videos on Youtube
Dan Sorensen
I am a full stack web developer primarily focused on the ASP.NET stack with and SQL Server. I like building fast, responsive pages and SQL Server development.
Updated on May 27, 2022Comments
-
Dan Sorensen over 1 year
Entity Framework 4.1 Code First works great with
SQLEXPRESS
onlocalhost
. However, I'm now ready to connect to a regularSQL 2008 server
.I created a new database "NewEfDatabase".
Then changed my "
ApplicationServices
"connectionString
inWeb.config
to point to my new database with integrated security.
But then I get this error:
"
CREATE DATABASE permission denied in database 'master'.
"So...
a) What permissions does EF 4.1 CF need on said SQL server to do its work?
b) Can I setup an empty database on
SQL 2008
for EF 4.1 CF, or do I have to let it do all that work for me? (I'm not sure my DBA would appreciate letting my EF app have rights to do anything outside a particular database) -
Dan Sorensen over 12 yearsNo I hadn't tried that. I'll give it a go. In that case should I migrate my data and schema from my local SQLEXPRESS? I had figured that it could build out the database in my target location. But forgot about the drop-create step it performs.
-
Slauma over 12 years@Dan: Yes, you must migrate the DB to the target instance if you set the initializer to
null
. In this case DB schema and EF model must match then, EF won't update or recreate the schema and probably throw exceptions if schema and model don't match. -
andriy over 12 yearsThe way I handle this is to create a list of names of developer PCs, and I only call SetInitializer (to re-create the database) if the app is running on one of those PCs. For any other machine, I assume the database will have been deployed there already.
-
Dan Sorensen over 12 yearsI stared a new question about steps to perform to move from development to migration here: stackoverflow.com/questions/5585446/…
-
Dan Sorensen over 12 yearsKyralessa: I like that concept. Thanks
-
Piotr Kula almost 9 yearsI am using database first with no initialiasers and getting this error. On my IIS Expres it works fine, then published to my local IIS it works fine, but then on the server... it throws this error?