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

相关内容

热门资讯

我来分享!掌中乐游戏中心吗(辅... 我来分享!掌中乐游戏中心吗(辅助挂)一贯存在有挂(2024已更新)(哔哩哔哩);1、掌中乐游戏中心吗...
wepok软件透明挂!wepo... wepok软件透明挂!wepoke可以来好牌(透明挂)wepoKe(原来真的有挂)准备好在wepoK...
微扑克wpk透视辅助!微扑克职... 微扑克wpk透视辅助!微扑克职业代打,微扑克app(确实有挂辅助挂)1、快速入门:当你通过点击开始按...
重磅来袭!心动麻将有挂么(透视... 重磅来袭!心动麻将有挂么(透视)一贯真的是有挂(2022已更新)(哔哩哔哩)1、起透看视 心动麻将有...
微扑克ai辅助工具!微扑克线上... 微扑克ai辅助工具!微扑克线上代打,WPK果然存在有挂,教你教程(有挂软件)是一款可以让一直输的玩家...
wepokeai代打!wepo... wepokeai代打!wepower插件(辅助透视)Wepoke(都是真的有挂);是一款可以让一直输...
教程辅助!越乡游斗地主有挂吗(... 教程辅助!越乡游斗地主有挂吗(辅助)原来有挂(2026已更新)(哔哩哔哩)1、很好的工具软件,可以解...
微扑克有辅助挂!微扑克有脚本,... 微扑克有辅助挂!微扑克有脚本,微扑克有辅助挂,解密教程(有挂方法)1、每一步都需要思考,不同水平的挑...
aapoker透明挂!wepo... aapoker透明挂!wepoke是机器发牌,wepoKe果然真的有挂,规律教程(有挂详情)这是由厦...
玩家科普!开心十三张辅助ios... 玩家科普!开心十三张辅助ios版(辅助挂)其实是真的有挂(2026已更新)(哔哩哔哩)1、开心十三张...