EF 4.1 CF: CREATE DATABASE permission denied in database 'master'

19,508

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)

Share:
19,508

Related videos on Youtube

Dan Sorensen
Author by

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, 2022

Comments

  • Dan Sorensen
    Dan Sorensen over 1 year

    Entity Framework 4.1 Code First works great with SQLEXPRESS on localhost. However, I'm now ready to connect to a regular SQL 2008 server.

    1. I created a new database "NewEfDatabase".

    2. Then changed my "ApplicationServices" connectionString in Web.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
    Dan Sorensen over 12 years
    No 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
    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
    andriy over 12 years
    The 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
    Dan Sorensen over 12 years
    I stared a new question about steps to perform to move from development to migration here: stackoverflow.com/questions/5585446/…
  • Dan Sorensen
    Dan Sorensen over 12 years
    Kyralessa: I like that concept. Thanks
  • Piotr Kula
    Piotr Kula almost 9 years
    I 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?