MichaelCleverly
MichaelCleverly

Reputation: 2543

Entity Framework: Seemingly random SQL Server connection error

I have an application using Entity Framework (database first) on top of a SQL Server database. The application is configured with default settings, like I've built all of my applications for the past couple of years.

However, with this particular application, when running on my windows server, I'm getting the following error now and then (quite often):

Exception message: 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).

The issue never occurs when the application is running locally through Visual Studio (connecting to the same remote SQL Server).

I know for certain that there is no error in the connection string, because as I said, the issue only happens sometimes. Other times it works just fint.

Furthermore, if I copy the entire web.config from the application running on my web server and paste it into my local development environment, it works just fine.

I thought it might be my webserver that was having issues accessing the SQL Server; but the other applications running on the same webserver, accessing the same SQL Server, are all working just fine.

I simply can't figure out what can be causing this, as Ive never experienced anything like it. What am I missing? What should I be looking for?

My connection string:

<add name="MyDbEntities" 
     connectionString="metadata=res://*/Model.MyModel.csdl|res://*/Model.MyModel.ssdl|res://*/Model.MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MyServer\MyInstance;initial catalog=MyDb;integrated security=False;User ID=MyUser;Password=MyPassword;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

Upvotes: 1

Views: 685

Answers (1)

MichaelCleverly
MichaelCleverly

Reputation: 2543

As it turns out, the issues were caused by my Owin context. As I don't use the ApplicationUserManager part of owin (I have my own custom tables for authentication), I do not need Owin to set it up for me. However, because I had the following code in my Startup.Auth, owin would try to connect to the DefaultConnection whenever the session expired:

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

I simply commented out the above (note: I can only do this, because my application does not use the ApplicationUser tables in the database), and it works.

Upvotes: 1

Related Questions