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

相关内容

热门资讯

十分钟辅助“亲友圈辅助吧”开挂... 十分钟辅助“亲友圈辅助吧”开挂(平台)辅助平台安装教程-有挂功能 >>您好:软件加薇13670430...
总算了解"闲来辅助最... 总算了解"闲来辅助最新版下载"开挂(平台)辅助平台有挂技巧-系统教程【无需打开直接搜索加薇13670...
四分钟辅助“大唐抓麻雀辅助”开... 您好:大唐抓麻雀辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特...
我来教大家"蜂娱辅助... 我来教大家"蜂娱辅助"开挂(软件)辅助软件了解有挂-分享教程;无需打开直接搜索打开薇:1367043...
三分钟辅助“暗宝破解器”开挂(... 三分钟辅助“暗宝破解器”开挂(安装)辅助安装系统教程-有挂秘笈 了解更多开挂安装加(13670430...
最新技巧"wepok... 最新技巧"wepoker开挂事件"开挂(安装)辅助安装有挂讲解-教你攻略;无需打开直接搜索薇:136...
教程辅助“丽水茶苑辅助下载ap... 教程辅助“丽水茶苑辅助下载app”开挂(透视)辅助透视总结教程-有挂分享 >>您好:软件加薇1367...
解密关于"wepok... 解密关于"wepoker俱乐部辅助"开挂(工具)辅助工具有挂详细-爆料教程;亲,wepoker俱乐部...
三分钟辅助“微友辅助器免费版v... 三分钟辅助“微友辅助器免费版v3.0”开挂(平台)辅助平台科技教程-有挂细节;无需打开直接搜索加(薇...
推荐攻略"贵州微乐自... 推荐攻略"贵州微乐自建房辅助软件下载"开挂(脚本)辅助脚本有挂规律-教你教程您好:贵州微乐自建房辅助...