SQL appsettings.json 中 ConnectionString 中的别名问题
Issues with SQL Alias in ConnectionString in appsettings.json
在我的 appsettings.json 中,当我使用此代码段时:
"ConnectionStrings": {
"CssDatabase": "Server=BLUEJAY\MSSQLSERVER2014;Database=CSS;Trusted_Connection=True;"
}
我可以按预期连接到数据库...没问题。
但是,当我将其更改为使用 SQL 别名 (CSSDB) 时,如下所示:
"ConnectionStrings": {
"CssDatabase": "Server=CSSDB;Database=CSS;Trusted_Connection=True;"
}
配置正确,因为我可以在 SSMS 中使用这个 SQL 别名来毫无问题地连接到数据库。
这个returns:
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
我正在使用 Microsoft.EntityFrameworkCore
。
由于有关 SQL 别名的信息存储在 Windows 注册表中,Microsoft 团队决定放弃对 .NET Core 的支持,因为它不是跨平台解决方案。这里是 link to discussion about it.
但是有解决方法(也来自这个讨论),对我来说效果很好,但请记住它仍然是 Windows 唯一的解决方案:
var builder = new SqlConnectionStringBuilder(config.ConnectionString);
var key = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") == "x86"
? @"HKEY_LOCAL_MACHINE\SOFTWARE\ Microsoft\MSSQLServer\Client\ConnectTo"
: @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\MSSQLServer\Client\ConnectTo";
var newSource = (string)Microsoft.Win32.Registry.GetValue(key, builder.DataSource, null);
if (newSource != null)
builder.DataSource = newSource.Substring(newSource.IndexOf(',') + 1);
config.ConnectionString = builder.ConnectionString;
如果您不在不同的 C# 中存储 ConnectionString class,您可以像我在下面所做的那样将 builder.ConnectionString
传递给 ConfigureServices 方法中的服务:
services.AddDbContext<AppDbContext>(
opt => opt.UseSqlServer(builder.ConnectionString));
在我的 appsettings.json 中,当我使用此代码段时:
"ConnectionStrings": {
"CssDatabase": "Server=BLUEJAY\MSSQLSERVER2014;Database=CSS;Trusted_Connection=True;"
}
我可以按预期连接到数据库...没问题。
但是,当我将其更改为使用 SQL 别名 (CSSDB) 时,如下所示:
"ConnectionStrings": {
"CssDatabase": "Server=CSSDB;Database=CSS;Trusted_Connection=True;"
}
配置正确,因为我可以在 SSMS 中使用这个 SQL 别名来毫无问题地连接到数据库。
这个returns:
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
我正在使用 Microsoft.EntityFrameworkCore
。
由于有关 SQL 别名的信息存储在 Windows 注册表中,Microsoft 团队决定放弃对 .NET Core 的支持,因为它不是跨平台解决方案。这里是 link to discussion about it.
但是有解决方法(也来自这个讨论),对我来说效果很好,但请记住它仍然是 Windows 唯一的解决方案:
var builder = new SqlConnectionStringBuilder(config.ConnectionString);
var key = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") == "x86"
? @"HKEY_LOCAL_MACHINE\SOFTWARE\ Microsoft\MSSQLServer\Client\ConnectTo"
: @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\MSSQLServer\Client\ConnectTo";
var newSource = (string)Microsoft.Win32.Registry.GetValue(key, builder.DataSource, null);
if (newSource != null)
builder.DataSource = newSource.Substring(newSource.IndexOf(',') + 1);
config.ConnectionString = builder.ConnectionString;
如果您不在不同的 C# 中存储 ConnectionString class,您可以像我在下面所做的那样将 builder.ConnectionString
传递给 ConfigureServices 方法中的服务:
services.AddDbContext<AppDbContext>(
opt => opt.UseSqlServer(builder.ConnectionString));