简体   繁体   English

ASP.NET核心EF代码首先appsettings.json

[英]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环境变量将当前环境设置为TestProd值。

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.

相关问题 ASP.NET Core appsettings.json 更新代码 - ASP.NET Core appsettings.json update in code 使用ASP.NET Core反序列化appsettings.json - Deserialize appsettings.json with ASP.NET Core AppSettings.json用于ASP.NET核心中的集成测试 - AppSettings.json for Integration Test in ASP.NET Core ASP.NET Core appsettings.json需要在磁盘上更新 - ASP.NET Core appsettings.json need to update on disk Serilog:RollingFile 在带有“appsettings.json”的 asp.net 核心中不起作用 - Serilog : RollingFile is not working in asp.net core with 'appsettings.json' ASP.NET Core appsettings.json 未加载正确的设置 - ASP.NET Core appsettings.json not loading the correct settings 用于 MSTest [ASP.NET Core 3.1] 的 appsettings.json - appsettings.json for MSTest [ASP.NET Core 3.1] 为 ASP.NET Core 配置引用 appsettings.json 中的另一个 json 文件 - Reference another json file in appsettings.json for ASP.NET Core configuration ASP.NET Core 3.1 将 appsettings.json 中的对象数组注入到注入的服务中 - ASP.NET Core 3.1 injecting array of objects from appsettings.json into an injected service 从ASP.NET CORE Web应用程序中的appsettings.json加载Hangfire配置 - Loading Hangfire configuration from appsettings.json in ASP.NET CORE web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM