appsetting.json转换错误项目没有合并,而被追加
创始人
2024-11-10 08:00:41
0

这个问题通常是由于appsetting.json转换文件中的属性名称不匹配而导致的。例如,属性名称在源文件和转换文件中不一致。

针对这个问题,可以使用以下代码示例解决:

1.在 appsettings.Development.json 文件中定义以下内容:

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AppSettings": { "Database": { "ConnectionString": "Server=(localhost);Database=MyDatabase;User Id=myuser;Password=mypassword" } } }

2.在 appsettings.Production.json 文件中定义以下内容:

{ "AppSettings": { "Database": { "ConnectionString": "Server=(prod-server);Database=MyDatabase;User Id=myuser;Password=mypassword", "UseConnectionPooling": true } } }

  1. 然后,在 Startup.cs 文件中添加以下内容:

public Startup(IConfiguration configuration, IWebHostEnvironment env) { Configuration = configuration;

var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables();

Configuration = builder.Build();

// Get the AppSettings section var appSettingsSection = Configuration.GetSection("AppSettings");

// Configure the database options services.Configure(appSettingsSection.GetSection("Database")); }

4.最后,在 ConfigureServices 方法中定义 DatabaseOptions,如以下代码所示:

public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => { options.UseSqlServer(Configuration.GetConnectionString("Database")); });

services.Configure(Configuration.GetSection("AppSettings:Database")); }

通过以上代码,添加的 Program.cs 的内容为:

using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging;

namespace TestConfiguration

相关内容

热门资讯

wpk到底有没有外挂!wpka... wpk到底有没有外挂!wpkai机器人,(WpK)切实真的是有挂,攻略方法(黑科技功能);1、wpk...
wepoke辅助插件!wepo... wepoke辅助插件!wepoke软件透明挂,(wepoker)切实是真的有挂,我来教教你(黑科技系...
德州之星透牌器!德州ai辅助a... 德州之星透牌器!德州ai辅助app,(德州ai)竟然有挂,必赢教程(黑科技辅助器)1、很好的工具软件...
aapoker外挂实测!aap... aapoker外挂实测!aapoker系统机制,(AAPOKEr)都是真的是有挂,实用技巧(黑科技辅...
微扑克辅助手机!微扑克如何让系... 微扑克辅助手机!微扑克如何让系统发好牌,(微扑克)一直有挂,力荐教程(黑科技辅助器);1、让任何用户...
wpk ai辅助!wpk外挂第... wpk ai辅助!wpk外挂第一视角,(WPK)确实真的有挂,科技教程(黑科技科技)1、这是跨平台的...
德州线上ai辅助神器!德州之星... 德州线上ai辅助神器!德州之星模拟器,(德州之星)其实是有挂,揭秘教程(黑科技攻略);1、该软件可以...
wepoke智能ai什么意思!... wepoke智能ai什么意思!wepoke软件透明挂多少钱,(WepOke)竟然有挂,解密教程(黑科...
WePoKe有挂吗!wepok... WePoKe有挂吗!wepokeai代打辅助,(wePokE)都是是有挂,详细教程(黑科技方法);1...
aapoker辅助是什么!aa... aapoker辅助是什么!aapoker记牌器,(aapoKER)切实存在有挂,实用技巧(黑科技工具...