Search code examples
sql-serverentity-frameworkasp.net-web-apidatabase-first

web api http get gives "Create Database permission denied in database master"


I have a database called XyzDB. Everything is done and I am happy with it.

I have a WebAPI project which used Entity Framework (I generated my entity models from my database, so not code first).

When I use fiddler to query a table with a simple Get http method. I get the error:

Create Database permission denied in database master

However, I don't want to create a database. I just want to do a simple get method and return a json result.

Any help would really be appreciated.


Solution

  • By default a DbContext creates the database if it does not exist, but does not perform a migration if the database already exist.

    In the constructor of your DbContext, just add Database.SetInitializer( null ) in order to prevent generating the database.

    You should also check your connection string in the App.Config because by default it connects to the master database. If it is Ole Db for Sql Server, add "initial catalog=[your database]" in order to connect to the right database.

    http://www.connectionstrings.com/microsoft-ole-db-provider-for-sql-server-sqloledb/