Build a SQL Server connection string without embedding credentials
SqlConnectionStringBuilder handles connection-string syntax and provides typed properties. It avoids manual concatenation, but it is not a secret store.
Keep the deployment's identity and connection settings outside generated entity classes. Prefer the authentication mechanism appropriate to the host instead of placing a password in source code.
Build an integrated-authentication string
Create a .NET console project and add Microsoft.Data.SqlClient. Replace Program.cs:
using Microsoft.Data.SqlClient;
var builder = new SqlConnectionStringBuilder {
DataSource = "localhost",
InitialCatalog = "Demo",
IntegratedSecurity = true,
Encrypt = true,
TrustServerCertificate = false,
ApplicationName = "ConnectionDemo"
};
Console.WriteLine($"Server: {builder.DataSource}; database: {builder.InitialCatalog}");
var connectionString = builder.ConnectionString;
Run dotnet run. Expect Server: localhost; database: Demo. This only constructs the string and does not connect. A real connection requires a suitable Windows/domain identity, database permission, and a trusted server certificate matching the server name.
Supply application configuration
In ASP.NET Core, retrieve builder.Configuration.GetConnectionString("App") and pass it to the database registration. The environment key ConnectionStrings__App can supply that value without changing generated files. Use development secret storage or the deployment's managed secret service when credentials are required. Avoid logging the resulting string.
For an existing .NET Framework LINQ-to-SQL application, pass the configured string to the generated DataContext constructor. Do not hand-edit generated DBML output merely to change a deployment connection. Modern EF Core uses options supplied to its context instead.
Official references


Author: Herman Schoenfeld