TeamCity Database Integration Tests - nunit

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

Related

Network error when scaffolding with working connection string

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;

Local SqlServer Database connection for API tests in azure devops pipeline build

I have a .Net Core 3.0 API and
a sql server 2019 database, running on the machine I develop on.
I have 2 API tests that need to connect to db, passing in visual studio test explorer
But on azure devops my pipeline build fails on those 2 API tests
I have set sqlserver for sharedmemory, named pipes and tcp/ip
did not change port 1433
have enabled remote connections in sql server
made a login and a schema for database login
connected my ssms through sql server authentication
have used the vs2019 sql server object explorer properties tab on the database to get the connection string for the db connected with sql server authentication
have stopped and restarted all sql services at regular times. Also my windows firewall is disconnected))
the failing build task in yml
- task: DotNetCoreCLI#2
displayName: 'Running Tests'
inputs:
command: test
projects: '**/*Tests.csproj'
arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'
get following error:
Microsoft.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: Named Pipes Provider, error: 40
- Could not open a connection to SQL Server)
---- System.ComponentModel.Win32Exception : The network path was not found.
connectionstring:
"Data Source=LAPTOP-SOMERANDOM-NAME;Initial Catalog=DBName;User ID=Admin;Password=*****;
Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
And if I add the port '1433' after Data Source I get following error:
Microsoft.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: TCP Provider, error: 0 - No such host is known.)
---- System.ComponentModel.Win32Exception : No such host is known.
Can I make it work?
Do I need a clone of my db of some sort in my pipeline?
Unfortunately I don't have a credit card and no more free credits for azure resource groups
also I don't work with keyvault, I'm trying to make the connection first, will make it safer later :)
Big Thanks in advance for reading and helping!

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;

EF7: error connecting to a local db database

I'm trying to create a new asp.net core app which uses EF7 to access a local db database and I'm having some problems with the connection string. I've installed the local db service and I have already created several instances:
PS C:\Users\luisabreu> sqllocaldb info
MSSQLLocalDB
ProjectsV12
ProjectsV13
v11.0
MSSQLLocalDB is running v12:
PS C:\Users\luisabreu> sqllocaldb info mssqllocaldb
Name: MSSQLLocalDB
Version: 12.0.2000.8
Shared name:
Owner: GRM\luisabreu
Auto-create: Yes
State: Running
Last start time: 05/04/2016 08:46:17
Instance pipe name: np:\\.\pipe\LOCALDB#518FD662\tsql\query
This is the connection string I'm passing to EF:
{
"usersCnnString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=UsersInfo;Integrated Security=True"
}
Btw, I've already tried this one too:
"usersCnnString": "Server=(localdb)\\mssqllocaldb;Database=UsersInfo;Trusted_Connection=True"
And here's the setup code I'm using for setting up ED dependencies:
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<UsersContext>(options => options.UseSqlServer(Configuration["usersCnnString"]));
Whenever I try to access the database,I end up with the following error:
System.Data.SqlClient.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 is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid.)
I'm surely doing something wrong, but what?
thanks!
Luis
Well, it was something simpler...don't forget to kill iis express whenever you change the config of an ASP.NET Core app so that you'll get the correct config data when you re-run the app. The problem was that when I pasted the path:
Server=(localdb)\\mssqllocaldb...
into the json file, VS 2015 automatically escaped the string, turning it into:
Server=(localdb)\\\\mssqllocaldb...
Yes, I've noticed it, but only after trying to load the app on the browser. I corrected the path but didn't kill the IIS Express instance I was using to test my app. So, the error message was absolutely correct because it was still using the old path (which was trying to reach the mssqllocaldb instance running on a localdb server).
Again, stupid error. Hoping that my mistake will save someone else's 30 mins in the future...
if I create a project with user accounts using VS 2015 with RC1 the project template generates the connection string like this:
{
"Server=(localdb)\\mssqllocaldb;Database=thedbnamehere;Trusted_Connection=True;MultipleActiveResultSets=true"
}
I would try making it closer to that, but also wondering if for some reason UsersInfo might not be a valid name, could be some keyword clash
I noticed that vs uses the "userSecretsId" from project.json as the dbname, not sure if that is required but might be worth trying

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.