这是我将如何做的一个例子......
编辑我的 Web.Config 文件
注意:名称:DefaultConnection、LocalConnection、livebsource、localdbsource、localMachineName
<connectionStrings>
<!--Live-->
<add name="DefaultConnection" connectionString="Data Source=livedbsource;Initial Catalog=livedbname;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
<!--Dev-->
<add name="LocalConnection" connectionString="Data Source=localdbsource;Initial Catalog=localdbname;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
对于 ApplicationDbContext
static string ServerName = System.Net.Dns.GetHostName();
//setting what connection string to use - tenary operator
static string ConnectionStringID = (ServerName == "localMachineName") ? "LocalConnection" : "DefaultConnection";
public ApplicationDbContext()
: base(ConnectionStringID, throwIfV1Schema: false)
{
}
在 Windows 7 及更高版本上,您可以按 Windows 徽标并搜索系统信息来获取 localMachineName,然后查找“系统名称”
这是我在 Visual Studio 2017 中的一个现场项目中制作的 YouTube 视频,它有效。
https://youtu.be/oKg6ewKhkYs