【发布时间】:2019-10-03 05:22:38
【问题描述】:
多年来我一直在 .NET Web 窗体中这样做:
Dim conn As New SqlConnection(f1.fUseThisConnection(Server.MachineName))
Public Function fUseThisConnection(ByVal strThisMachine As String) As String
If strThisMachine = "T61" Then
fUseThisConnection = ConfigurationManager.AppSettings("DevHome")
Else
fUseThisConnection = ConfigurationManager.AppSettings("Production")
End If
End Function
AppSettings 在 Web.config 中:
<appSettings>
<add key="Production" value="server=xxx;uid=xxx;pwd=xxx;database=xxx;pooling=false" />
<add key="DevHome" value="server=xxx;uid=xxx;pwd=xxx;database=xxx;pooling=false" />
但我还没有为我的 C# Razor Pages 搜索和找到如此简单的东西。 Microsoft howto 中的任何内容都让我大吃一惊。
这是我的 Razor Pages 项目中的 appSetting.json:
"ConnectionStrings": {
"DefaultConnection": "Data Source=Txx\\SQLxxx;Initial Catalog=xxx;Persist Security Info=True",
"Production": "Data Source=xxx;Initial Catalog=xxx;Integrated Security=True;User ID=xxx;Password=xxx"
},
这里是我引用连接字符串的地方——它是硬编码的——但我希望有一种方法可以执行“如果”语句:“如果环境 = 随便什么,则获取此连接字符串”。
services.AddDbContext<MyDbContext>(
options => { options.UseSqlServer(@"Data Source=Txxx;Initial Catalog=xxx;Integrated Security=True"); });
【问题讨论】:
标签: .net-core connection-string razor-pages