3

I am unsing entity framework in my web application. Earlier everything was working fine. But then I deleted database from Sql Server. After that whenever I run the application, I get the following exception :

The underlying provider failed on open entity framework  

Isn't it true that if we delete the database from Sql Server and again run the application, database is again created?

halfer
  • 19,824
  • 17
  • 99
  • 186
Nitish
  • 13,845
  • 28
  • 135
  • 263

3 Answers3

3

Entity framework will recreate your database on application start assuming the following:

  • You have permission to create databases in SQL
  • A connection string is specified correctly (if it was working before this should be fine)
  • You are using CodeFirst or ModelFirst strategies for databases (ie if you generated an EDMX off an existing database it wont recreate it for you)
undefined
  • 33,537
  • 22
  • 129
  • 198
  • The error that you have gotten normally relates to either a permission or connection based issue. I would recommend troubleshooting around weather the user running your app (or in the conn string) can both access and create the database. Try doing it manually using those credentials. – undefined Aug 03 '12 at 10:36
  • Yeah check you can login (from the machine running the app, SSMS works for me), create a db and run a create table script, its probably also worth confirming that you have deleted/moved the .mdf and .ldf files on the SQL box so you dont get a name conflict. – undefined Aug 03 '12 at 10:39
  • Also just make sure you havent applied another behavior for EF, the following is default: `System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists());` see http://stackoverflow.com/a/6131926/1070291 – undefined Aug 03 '12 at 10:42
  • Yes, I am able to add database and create table from SQL server. – Nitish Aug 03 '12 at 10:42
3

Can you show some details about connection string. I basically want to check the authentication you are using. Demo for setting a typical authentication : http://msdn.microsoft.com/en-us/library/ff649314.aspx

Another possibility can be with connection pooling. Try explicitly closing the connection(if there are any open connections)

db.Database.Connection.Close();
NG.
  • 5,695
  • 2
  • 19
  • 30
0

I had a similar error and it was due to the connection problem. Usually you need to open your *.EDPS file under your entity (EDML) and check your connection string and make sure it has got a correct setting especially your Default Oracle home.