Network error when scaffolding with working connection string - entity-framework-core

I have a connection string to a SQL Server DB.
This connection string works fine when used in the application.
It also works fine when used in SQL Server Management Studio (SSMS).
It fails with a network error when I try to use it to do DB-first approach scaffolding with entity framework via Scaffold-DbContext
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
I'm not sure what to do here since the connection string works in other contexts.
The connection string looks like this:
"Data Source=dbserver\\db01;Initial Catalog=mydb;Persist Security Info=True;User ID=user;Password=password; Connect Timeout=60;Connection Timeout=60;Connection Timeout=60;Encrypt=False;TrustServerCertificate=True;"
This is probably from a VPN that is required to connect to the DB. VS is probably not using the VPN properly when trying to connect.
How do I troubleshoot this error?

I figured it out.
This is the issue
dbserver\\db01;
That second slash is a string escape sequence.
It's not needed here and removing it solves the issue.
So it should be
dbserver\db01;

Related

Can't connect to SQL Azure from EF, but can from SSMS

We have an Azure SQL server running which we use for our Azure Web App. The Web App can connect without a problem. We're trying to remotely update the database with the latest EF migrations via a C# console application.
However we get the following exception.
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)
We tried connecting from the same PC with SSMS and we got in without any problems. And yes, before anyone asks we whitelisted the IP in Azure, else we wouldn't even had access via SSMS. We also tried with and without "tcp:" in the connectionstring.
This is the connectionstring we use in the Web and Console Application:
Server=tcp:{servername}.database.windows.net;Database={dbname};user id = {userid};password = {password};MultipleActiveResultSets=True;
In SSMS we just use {servername}.database.windows.net and use the credentials.
Thanks in advance.
We haven't been able to solve this issue. We instead chose to go with ReadyRoll, which suits us fine.
Azure SQL requires secured connection. You have to add the following to your connection string:
Trusted_Connection=False;Encrypt=True;

TeamCity Database Integration Tests

I am setting up a TeamCity build to run some database related Nunit tests. These tests would need a Sql server connection to run.
I am trying to pass such a connection string as a Test parameter.
I have created a build variable called
Database_Connection_String
And I have set it to
Data Source=ANDROID-PC\SQLEXPRESS;Initial Catalog=TestDb;;User ID=TestDbAdmin;Password=***********
Further I have passed the test parameter to the Nunit3-console.exe in the following manner:
--params=TestDatabaseConnectionString=%Database_Connection_String%
After running the build, i am being shown the following error:
[My_TEST_NAME]
OneTimeSetUp: System.Data.SqlClient.SqlException : 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 Sql server is running and I am able to connect to it using the same connection string from my app on the same machine. It is just that the Nunit tests are not picking up the connection string.
I have tried encapsulating the connection string with Double and Single quotes, but that didn't work either.
Is there a way to pass connection string as a test parameter to the Nunit tests?
Currently, there is no way to pass in a parameter containing a semi-colon with the --params option. This is something that will hopefully be fixed in time.
See the GitHub issue: https://github.com/nunit/nunit-console/issues/23

ASP.NET core connection string - use AWS RDS

I would like to switch from using a local db in my web app, to aws relation databases (RDS).
I have connected to the aws RDS via mysql workbench.
In order to make this switch, all I have changed is the connection string. But it doesn't work. (it hangs for 30 seconds then throws HTTP Error 502.3 - Bad Gateway).
the local db: =Server=localhost;database=ef3;uid=root;pwd=123456;
But when I change it to Server=test2.xxx.ap-southeast-2.rds.amazonaws.com;database=test2;uid=xx;pwd=xx;port=3306;, I get the following output from the console.
https://pastebin.com/raw/qA0py6t0
The last line says: "Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware","{OriginalFormat}":"An unhandled exception has occurred while executing the request"}}}
How do I fix the connection string, to connect to the AWS RDS?
Thanks guys
The connection seems fine, seems like that database doesn't contain the expected data.
From the trace:
MySql.Data.MySqlClient.MySqlException: Table 'test2.Events' doesn't exist

EF code first generated DB can be updated from code, but cannot retrieve data

I am using EF code first to generate my DB, it's worked. When I tried to read data from a table via:
var l = db.MyTable.ToList();
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 got this error. I usually because of wrong connection string. But my connection string is right. I use it to update my database and any change is right.

SQL30082N Attempt to establish connection failed with security reason 5

I'm getting this error when i try to connect to DB2 Database server using OLEDBConnection in Visual studio 2008 windows application developed in c#:
SQL30082N Attempt to establish connection failed with security reason 5
I'm using Microsoft OLEDB Provider for DB2. When i try to connect i am getting the above error.
I am using the below connection string for connecting to DB2
ConnectionStr = "Provider=IBMDADB2;Database="SampDB";Hostname=114.968.25.61;Protocol=TCPIP;Port=60001;Uid =testuser; Pwd=!DB2User!;";
I am 100% sure that this userid is correct. My DB2 client provider version is 8.0
In my desktop i am getting this error. but this windows application works well on all others machine. Please help me in fixing the issue.
According to Info Center, reason code 5 for SQLSTATE SQL30082N means that the UserId is missing.
I think what may be the issue (if you copied and pasted your exact connection string) is that there is an extra space after Uid.
If you're using .NET, then you may want to look at the OleDbConnectionString class. It will format and escape the connection string as needed.