1

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.

5
  • 1
    The database exists? Are you using some database initializer like DropCreateDatabaseAlways or DropCreateDatabaseIfModelChanges? Commented Jul 20, 2015 at 19:56
  • I tried to search through the solution for the phrases you specified, but they are no where to be seen. So I guess the answer is no. Commented Jul 20, 2015 at 20:13
  • 1
    Then your database doen't exist at the specified server at the connectionString, and the user has no permision to create It Commented Jul 20, 2015 at 20:14
  • I just deleted the EF model and commented all the connection strings and then re-added it. Still same error. But the con string has to be right if I can read the tables and add them to the model etc. Commented Jul 20, 2015 at 20:31
  • 1
    Yeah...I'm a bit of a dumb head. :) I was connecting to my test server instead of my live one. The database on the test has a different name. Hence the errors. You were right, the database didn't exist. Thanks for your answers. Commented Jul 20, 2015 at 20:39

1 Answer 1

3

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/

Sign up to request clarification or add additional context in comments.

1 Comment

I feel a bit dumb, I tried your Database.SetInitialaizer, after I got another error, which led me to the solution. Guess what...I was connecting to the wrong server and the database didn't exist. :) Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.