[英]ASP.NET Core EF Code First appsettings.json
I'm looking for a method to switch dynamically between production and test environment. 我正在寻找一种在生产和测试环境之间动态切换的方法。
I have two different connection strings to MSSQL databases. 我有两个不同的连接字符串到MSSQL数据库。 I want to dynamically pass this to my dbContext: 我想动态地将它传递给我的dbContext:
services.AddDbContext<ViggrContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("TestDatabase")));
I have two types of publish profiles, one for Test and another for Production environment. 我有两种类型的发布配置文件,一种用于测试,另一种用于生产环境。 In this profile I choose a connection to the database. 在此配置文件中,我选择与数据库的连接。 Ofcourse the Test profile points to the TestDatabase connection string and the Production profile points to the Production Database. 当然,测试配置文件指向TestDatabase连接字符串,生产配置文件指向生产数据库。
But how can I dynamically load the Startup.cs class in this section of the code? 但是,如何在代码的这一部分动态加载Startup.cs类?
services.AddDbContext<ViggrContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("TestDatabase")));
Do you have any suggestions ? 你有什么建议吗 ?
You can configure different environment connection strings in different appsettings files like this- 您可以在不同的appsettings文件中配置不同的环境连接字符串,如下所示
For test environment, use appsettings. 对于测试环境,请使用appsettings。 test .json 测试 .json
"Data": {
"ViggrContext": {
"ConnectionString": "" /*<<== TestDatabase connection string */
},
For prod environment, use appsettings. 对于prod环境,请使用appsettings。 prod .json prod .json
"Data": {
"ViggrContext": {
"ConnectionString": "" /*<<== ProdDatabase connection string */
},
Use ASPNETCORE_ENVIRONMENT environment variable to set current environment as Test or Prod values. 使用ASPNETCORE_ENVIRONMENT环境变量将当前环境设置为Test或Prod值。
In Startup, you can use like this- 在Startup中,您可以像这样使用 -
services.AddDbContext<ViggrContext>(options =>
options.UseSqlServer(Configuration["Data:ViggrContext:ConnectionString"]));
See if this helps. 看看这是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.