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

相关内容

热门资讯

教程辅助“九游破解版真的假的”... 教程辅助“九游破解版真的假的”开挂(下载)辅助下载2026新版教程-有挂透明挂;无需打开直接搜索打开...
分享认知"禅游游戏辅... 分享认知"禅游游戏辅助"开挂(神器)辅助神器讲解有挂-扑克教程;无需打开直接搜索加(薇:136704...
开挂辅助“拱趴大菠萝自动计算机... 开挂辅助“拱趴大菠萝自动计算机器人”开挂(脚本)辅助脚本AI教程-有挂教学;打开点击测试直接进入微信...
科普分享"星悦游戏辅... 星悦游戏辅助论坛 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1...
详细辅助“宁夏微乐科技辅助”开... 详细辅助“宁夏微乐科技辅助”开挂(平台)辅助平台教你攻略-有挂方针>>您好:软件加136704302...
玩家必看科普"微信小... 微信小程序家乡大贰辅助器 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理...
技术分享"决战十三辅... 技术分享"决战十三辅助器"开挂(透视)辅助透视真是有挂-揭秘攻略;亲,决战十三辅助器这款游戏原来确实...
开挂辅助“至友互娱情怀房卡”开... 开挂辅助“至友互娱情怀房卡”开挂(透视)辅助透视安装教程-有挂教学;无需打开直接搜索打开薇:1367...
终于清楚"朋朋政和辅... 朋朋政和辅助器免费在哪里 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理...
透视安装"决战卡五星... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...