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.

Francis Rodgers
  • 4,565
  • 8
  • 46
  • 65
  • 1
    The database exists? Are you using some database initializer like DropCreateDatabaseAlways or DropCreateDatabaseIfModelChanges? – Fals Jul 20 '15 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. – Francis Rodgers Jul 20 '15 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 – Fals Jul 20 '15 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. – Francis Rodgers Jul 20 '15 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. – Francis Rodgers Jul 20 '15 at 20:39

1 Answers1

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/

Anthony Brenelière
  • 60,646
  • 14
  • 46
  • 58
  • 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. – Francis Rodgers Jul 20 '15 at 20:56