I have an ASP.NET MVC web application using Entity Framework to connect a SQL Server 2012 configured with high availability. It is configured to automatically fail over to a secondary server and this works well when trying to connect with SQL Server Management Studio. I could confirm this, and read or write data to database.
Somehow this doesn't happen from the application and I got the following error:
The target database ('DbName') is in an availability group and is currently accessible for connections when the application intent is set to read only. For more information about application intent, see SQL Server Books Online.
This is the connection string that I am using:
<add name="DataModel"
connectionString="Data Source=DBCL01;Initial Catalog=Database;User Id=user; Password=password;"
providerName="System.Data.SqlClient" />
Anyone knows how could I solve this issue?
The problem is that your application is not being directed to the primary (writable) server, but is redirected to read-only secondary replica.
I had a similar problem, my problem was that the connection string was lacking ApplicationIntent=ReadWrite and that the availability listener wasn't being resolved good by the DNS.
Also check this AlwaysOn Availability Groups Connectivity Cheat Sheet since bad setting of connectivity options for availability group can be a cause for connectivity errors and denial for connection to primary server.
Related
I am developing a web application, using Visual Studio 2013 and a SQL SERVER database created on Azure.
Almost every time that I took more than 30 seconds to build and re-run the application (F5) I get this error below. The connection sql connection to SQL Timed-out and this is the error raised.
[Win32Exception (0x80004005): The network path was not found]
I retry the connection and the problem goes away.
The point is that I am loosing so much time on this issue.
INFO ADDED:
I have the SAME issue on SQL Management, first attempt network not found, second attempt it works.
I have seen other computers, running other apps (.net based) on the company with the same behavior on a different SQL Azure server, so, in my mind is something related to azure dns.
I have two SQL connections, to the same server, one for OWIN context, and another used by Entity Framework 6. Both MARS enable.
I don't feel to having the same issue browsing any other website.
My computer is part of an enterprise domain (that I had myself setup, and I am not an expert on Windows Server)
I am using a Macbook with ThunderBolt dongle for ethernet.
My connection strings
<connectionStrings>
<add name="Container" connectionString="metadata=res://*/Models.MakeItMine.csdl|res://*/Models.**********.ssdl|res://*/Models.**********.msl;provider=System.Data.SqlClient;provider connection string="data source=**********.database.windows.net;initial catalog=db**********;persist security info=True;user id=**********;password=**********;MultipleActiveResultSets=True;App=EntityFramework;Connection Timeout=3;ConnectRetryCount=3;ConnectRetryInterval=1"" providerName="System.Data.EntityClient" />
<add name="OWINConnection" connectionString="data source=**********.database.windows.net;initial catalog=db**********;persist security info=True;user id=**********;password=**********;MultipleActiveResultSets=True;App=MakeItMine;Connection Timeout=3;ConnectRetryCount=3;ConnectRetryInterval=1" providerName="System.Data.SqlClient" />
</connectionStrings>
My IP settings
What I tried
Disabled AVG Components
Disabled Windows Firewall for Home/Enterprise
networks
Changed my dns to Google's dns (8.8.8.8/8.8.4.4), if I open
nslookup and try to resolve my database path, it works every time.
Changed my sql connection parameters: "Connection Timeout=3;ConnectRetryCount=3;ConnectRetryInterval=1", so it takes less time to raise the error, and I can retry faster and not loose so much time.
Azure doesn't permit me "pinging" the server, since never returns. So, I am really in trouble on how to troubleshoot/monitor this issue.
Any suggestions on how to troubleshoot this are appreciated.
I did not found the exact problem neither the perfect solution yet, but I do know its a DNS related problem.
So, my fix by now is changing my windows HOSTS file and fixing my database dns to its ip. It's working for now and saving me a lot of debugging time.
In my web.config I have defined following connection string for a Database that not existing in sqlexpress (code first entity framework approach)
<connectionStrings>
<add name="cityconnectionstring" connectionString="Data Source=.\sqlexpress;Initial Catalog=CityPolulation;Integrated Security=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
So once I define this on web.config file I can see cityconnectionstring listed under Server Explorer window Data Conncetion in Visual Studio.
But When I trying to Right Click on above connection string and hit Refresh I'm getting following error in Visual Studio.
Server Explorer Error
server explorer unable to perform this operation see below for
details.
Cannot open database "CityPolulation" requested by the login. The
login failed. Login failed for user 'PC-name\username'.
All the answers related to above error are for existing DBs.
How to set permission that can connect to sqlserver for not existing DBs under Windows Authentication.
What are the Settings I should look in IIS or SQL server Management Studio
When the connection is verified it checks that you have access to the database. If the database does not exist then you cannot verify the connection.
If as your question's title suggests, you want to create the database, then you will still need to connect to a database first. IF you have permissions to create a database then you will certainly have access to the "master" database.
However, I hope that you are not expecting all users to be able to create a database as that would mean that all of your users have far too many permissions to the server!
EDIT Change the connection's database to "master". That will prove that you can connect.
Then manually (in SSMS or whichever tool you use) Create the new database on the server.
Now modify your connection to use the new database connection. You should now be able to connect.
Entity Framework is not capable of creating databases. However, once you have created the database it can create tables and data.
In an attempt to do some basic profiling on our Azure SQL Server (V12) we enabled Auditing using the Azure portal.
Not many seconds later I had an angry developer on my hands that could not connect to the database using his client (HTML5 page).
I really couldn't see the connection, but since enabling auditing was the only change, I tried disabling Auditing again, and sure enough now the client could connect.
Further info:
I could access the SQL server using SSMS just fine.
The problem was only on the TEST environment from the customers network, not from our DEV environments (which is from Visual Studio on the developers own laptops).
Can anyone tell me why enabling auditing can break this? We tried doing it on other databases too, and sure enough: when I enabled auditing, the connection could not be established...
According to this page, if you want auditing on, the FQDN connection strings need to be changed:
Original server FQDN in the connection string: (server name).database.windows.net
Modified server FQDN in the connection string: (server name).database.secure.windows.net
the following error appears when I want to launch the Security tab in ASP .NET Configuration site for my ASP NET MVC 4 project:
http://s14.postimg.org/jvizqragv/error_sql.png
I know that there are a lot of questions related to that error, but i tried a lot of the tips for solving this (I tried the suggested solutions here - http://blogs.msdn.com/b/walzenbach/archive/2010/04/14/how-to-enable-remote-connections-in-sql-server-2008.aspx but the error is still here).
But let me explain what are my databases in the solution. I have a local database (an .MDF file in App_Data) and a database for users authentication which was generated by ASP NET MVC. They are all placed in the App_Data folder as I'm trying to work on this solution on different machines. The strange thing is that on my laptop at home there is no such problem - I am able to create roles, explore users and all the functionality in the Security tab and I think there might be some problem with the SQL Server on my desktop machine - I use the Express edition on my laptop and the Developer edition on the other machine where things don't work.
My connection strings are the following:
<add name="FootballTransfers20130717145140Context" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\FootballTransfers.mdf;Integrated Security=True;MultipleActiveResultSets=True;Connect Timeout=30"
providerName="System.Data.SqlClient" />
<add name="DefaultConnection" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\DefaultConnection.mdf;Integrated Security=True;MultipleActiveResultSets=True;Connect Timeout=30"
providerName="System.Data.SqlClient" />
The first one is for the Entity framework and is related to the database where I store my application specific data. The second one is for the users administration database generated by SQL. Without the second connection string I'm not able to open any action of the Account controller.
It's very strange because it works on one machine and not on another. I was wondering if there's something wrong with the connection string, but I guess there's something with the SQL Server. As I said I tried various solutions but they didn't work.
LocalDB is a feature of SQL Server Express only. You will have to install SQL Server Express on the machine or import your database to your SQL Server and change your connection strings.
In production, you would probably have to move to a full SQL Server edition anyway.
I added the roles i was trying to make in code in the AccountController and now I'm able to register users with roles, but the problem that appeared now is that when I put the Authorize attribute on some action so that only users with certain roles can request them, the following error appears:
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)
When I remove the attribute so anyone can access it, the action works fine. I found some article on the net saying that providers must be specified in the config, but some errors kept appearing that those tags are unknown.
I have checked the instance name, auto close is set to true, allow remote connections on the server is checked. The server is running when I open the SQL Server configuration manager. I have even rebooted. I have created this db the same way as all others. I use Entity Framework and have checked the names in the web.config and they match. This is the default connection string from the wizard - I use for testing before I deploy. I just can't think of anything else to check to figure out why it won't connect. Working inside SQL Server everything is fine.
Here is the general 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: Shared >Memory Provider, error: 40 - Could not open a connection to SQL Server)
Config:
connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string='data source=.\SQL_1;attachdbfilename="C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.SQL_1\MSSQL\DATA\A_db.mdf";integrated security=True;connect timeout=10;user instance=True;multipleactiveresultsets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
Thanks in advance.
Let me preface this answer by saying that proper setup of a SQL instance is not as easy as Microsoft would like you to think with the entity framework. It's a little bit involved and requires that you put your DBA hat on for a little bit.
The error you have indicates that the web instance is attempting to connect to the SQL server using Windows Integrated Security. This will work fine if (a) the windows user that the process is running as (which can be configured in IIS) is authorized to log on to the SQL server and has a valid login in the database and (b) if the SQL server is on the same machine or in the same domain as the IIS server.
In light of this, I recommend using SQL Server authentication. If you need to know how to do this, I recommend searching for "SQL Server Authentication setup" - here is an article that I found which might help you set this up.
http://msdn.microsoft.com/en-us/library/aa337562.aspx
In general, I recommend taking the following actions:
Connect to the sql server using MS SQL Management Studio.
Permanently attach your database, then use the Initial Catalog property on your connection string rather than AttachDbFileName
Then set up your login username and password on the SQL server, and create a login in the database for it.
Make sure your login can only execute the stored procedures you want it to execute. Deny it access to running sql statements.
You will also need to add the username and password to your connection string, and set IntegratedSecurity=false.
Let us know how things go once you get your SQL server set up properly.
This error means that your provider code cannot find the SQL Server. If you have checked the server instance name (it should be <yourLocalServer>\SQL_1), then it could be the attachdbfilename= parameter, as this is a really unreliable way to specify the database to connect to (you should be using the Database Name, not the file name), because there are about a hundred reasons that the file name could change that have nothing to do with your application.