Entity Framework: Seemingly random SQL Server connection error - c#

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="data source=MyServer\MyInstance;initial catalog=MyDb;integrated security=False;User ID=MyUser;Password=MyPassword;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />

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.

Related

WCF database connection working but fails when accessing database from WPF

I have a weird error occurring i've hit a brick wall with. I have my WCF service hosted on my web host, I can access this fine. I have a database set up on my web host and I have created the connection string to the database within my WCF web.config.
When I test the database connection to the database on the live server from my local WCF project the connection works fine. When I run my WPF application, it creates a contract with my WCF service (WPF app is local and WCF is live) I get the
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)"
I really don't understand how I can connect to the database from my local WCF project but not from my live one? Anyone got any tips on how to either resolve this or even how to identify where the issue is occurring?
RESOLVED: Apologies for wasting your time but I had the database connection string hardcoded in a configuration file as well as in the web.config which is what was causing the problem. I meant to fix this before and forgot all about it.

Unable to connect to SQL Server database from IIS hosted application using SQL authentication

I have been developing an application in VS 2012 using Entity Framework 6.0 for my backend connectivity. I set up a connection to my SQL Server 2008 database and successfully configured the Entity Framework model (database first) without issue. However, when I attempted to connect to the database when the application is running in IIS, I am consistently getting an exception.
There are several layers of exceptions, but the innermost exception appears as:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I have tried extending the connection timeout to 60, but this did not have an impact, nor does it take nearly as long when connecting through other means, so I do not believe it is related to timing out. The calling application is a Web API project and hosted in local IIS and I have copied the exact connection string from my EF model project to the web.config file in this project, which looks like the following:
<add name="ConnectionName"
connectionString="metadata=res://*/ModelName.csdl|res://*/ModelName.ssdl|res://*/ModelName.msl;provider=System.Data.SqlClient;provider connection string="data source=server,portnumber;initial catalog=dbname;persist security info=True;user id=user;password=password;MultipleActiveResultSets=True;""
providerName="System.Data.EntityClient" />
I have successfully connected to this connection using a standard SQL Client connection (not from IIS app), through SQL Server Management Studio, and even using the same EF connection string when testing locally, but for some reason I cannot do so while connecting through the code when the app is hosted in IIS. I can confirm that the DbContext class is using the appropriate connection information while debugging.
Update:
I've discovered that the issue is NOT specific to Entity Framework. The issue is occurring when I test with a SQL client as well. However, it does occur with every application hosted in IIS and not just my Web API application. Still not able to figure exactly what the issue is, however.
In this connection string, the SQL Server is called server and the port number is portnumber. Clearly you are not using this when connecting through SSMS. Why don't you alter this connection string to match whatever works in SSMS.
It should probably at least be localhost instead of server:
connectionString="
metadata=res://*/ModelName.csdl|
res://*/ModelName.ssdl|
res://*/ModelName.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=localhost;initial catalog=dbname;
persist security info=True;
user id=user;
password=password;
MultipleActiveResultSets=True;""
(carriage returns are just for clarity)
I discovered the reason this was occurring. In IIS, I had to enable Windows Authentication and disable Anonymous Authentication for my application. I'm not sure why I was unable to connect to the SQL Server using SQL Authentication without my Windows identity being authenticated, but this was the solution that allowed me to connect through IIS.

SQL error 26 visual studio

Hello I've been getting this error message for some time now.
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)
I'm developing an application in MVC4 with a local SQL Server Database in Visual Studio.
The error appears some times and some times not. The project is under version control and when I clone it down on another computer it mostly seems to work. I am able to access the database throught the Server Explorer tab - so I'm figuring it can't be a server issue.
Does anyone have any suggestions to what can be the cause of this. I'm at wits end and have tried all sorts of things like deleting the database and have my application recreate the database.
These are my connection strings:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=default-FleaPortal-20131021113901;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\default-FleaPortal-20131021113901.mdf" providerName="System.Data.SqlClient" />
<add name="UserConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=user-FleaPortal-20131108090400;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\user-FleaPortal-20131108090400.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
Any help is appreciated.
Since your server appears to be local, that should rule out any network problems.
That leaves problems with your service, so my guess would be that:
Your SQL Server service is configured to start up on-demand, and
It's not always starting up fast enough for the connection timeout
This would mean that the first time you try to connect after a system reboot or a SQL Server restart it would sometimes throw that error.
If this is the problem, then recommended solutions would be to either change the service startup from "Automatic (Delayed Start)" to "Automatic", or to extend the length of your connection timeout.
I think my problem involved having multiple Context-classes.
I had one class implementing DropCreateDatabaseAlways that seeded some data and another class that used Code First Migrations. These databases conflicted in some weird way.
I'm not fully aware what the actual problem was, but putting it all in one context class and moving my seed data to the Code First Migrations solved it.
So there you have it, if you did like me and run into this problem. Do it this way instead.
This really caused me a lot of headache.
Maybe your application does not have enough rights to access the database.

Connection string not working on remote server

I am working on a C# ASP.NET website that needs to connect to a database. I have all the connection strings set up and they work locally, but when I try to connect from the server I get an error that looks like this
[Win32Exception (0x80004005): Access is denied]
[SqlException (0x80131904): 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
The connection string:
<add name="Default" connectionString="Server=server;Database=database;User ID=user;Password=pass;" providerName="System.Data.SqlClient"/>
Would the app pool identity block the connection even when the username and password are supplied directly?
EDIT: There seems to be confusion about what my problem actually is. I can connect to the SQL server with the same connection string from my local ASP Development server just fine. The only issue is that I cannot connect to the SQL server from the production ASP server.
You connections pool is not the problem -- you have a basic connectivity issue
You may have a bad connection string -- everything must be typed correctly -- you did not supply your actual credentials -- presumably for good reasons, unless this is your actual connection string, in which case you need to fix it with your correct connection settings)
You may be port blocked by a firewall whether personal, corporate, ISP, etc.
The DB may not be accepting network connections
The DB server may not be running
McAfee A/V has been known to cause problem.
I'm sure I have overlooked a few possible problems.

Can't connect to SQL server on LINQ to SQL web page

On my prod server, I have 2 instances of SQL server, SQL Server 2000 is the default instance, and I have \SQLEXPRESS2008 as an additional named instance. In my web project, I need to connect to the default, SQL 2000 instance.
I'm using LINQ to SQL in Visual Studio 2008.
In the Server Explorer in Visual Studio, I can create a connection to the DB, see all the tables, correctly generate the LINQ to SQL classes, etc.
In Management Studio I can connect to the DB, see everything, edit, etc.
When I try and run my web project code, however, I get:
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: Named Pipes
Provider, error: 40 - Could not open a
connection to SQL Server)
My connection string in the web project looks like:
<add name="myConnectionString" connectionString="Data Source=myServer.com;Initial Catalog=myDB;Persist Security Info=True;User ID=sa;Password=XXXXX" providerName="System.Data.SqlClient" />
(I'm testing with the sa account out of desperation).
I have other web projects running fine and utilizing the DB, but this is the only one using the LINQ to SQL classes (if that matters)
Help appreciated
Jonathan
If the web server is the same as the SQL server, just use . as the server name; much simpler.
If the web server us not the same as the SQL server, ensure that there is line-of-sight between these servers through any firewalls etc. Also ensure that the server name resolves (in many ways, it is simpler to use the IP address in the connection string if name resolution cannot be guaranteed).
This may be the same thing that happened to me and is one of the most annoying things about LINQ to SQL. Each time you make a change to your dbml, it reverts to the connection used by the server explorer in VS. You must right-click the design surface then manually change the connection back to "myConnectionString", recompile and run.

Categories

Resources