Jay
Jay

Reputation: 1033

Dll with Entity Framework unable to connect to database

I have a separate dll that handles connections to the database and thus holds the models and the api context. the app.config 'acts' like the web.config and holds the connection details However I keep getting the following error

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

So I copied the connection string from another more traditional mvc web.config file and the error still occurs.

Oddly, the following works:

 update-database -verbose

My app.config has the following connection string:

<add name="DefaultConnection" 
     connectionString="data source=(LocalDb)\v11.0;initial catalog=APIEntities-01;persist security info=True; Integrated Security=SSPI;MultipleActiveResultSets=True" 
     providerName="System.Data.SqlClient" />

and the dbContext points to it

 public ApiContext() : base("DefaultConnection")
 {
 }

As I say, this works in a traditional MVC web.config, but using a dll that references Entity Framework and such like it seems to not be able to locate the database. Any suggestions?

EF 6

Upvotes: 0

Views: 684

Answers (2)

Mahabub Rabbani
Mahabub Rabbani

Reputation: 134

Some times its hapen when sql server failed to open. Then open sql server configuration and then select (sql server Services) see on state which are stopped or manual...right click on then and make start...then the problem will be solved.

Upvotes: 0

Igor Damiani
Igor Damiani

Reputation: 1927

I suppose that the connection string because the "data source=(LocalDb)\v11.0;initial catalog=APIEntities-01" is a path local to your PC. You must provide the real SQL Server that you need to use. See this:

Change default location of LocalDb

Upvotes: 1

Related Questions