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

相关内容

热门资讯

透视计算!微乐小程序免费黑科技... 透视计算!微乐小程序免费黑科技,微乐家乡麻辣自建房辅助app(透视)确实是真的挂(哔哩哔哩)1)微乐...
四分钟神器!微乐小程序免费黑科... 四分钟神器!微乐小程序免费黑科技,微信小程序微乐家乡辅助器(开挂)切实是真的挂(2026)1、上手简...
透视存在!微乐小程序黑科技,微... 透视存在!微乐小程序黑科技,微乐家乡脚本助手(透视)都是真的有挂(哔哩哔哩)1、进入到微乐家乡脚本助...
第7分钟教程!微乐小程序免费黑... 第7分钟教程!微乐小程序免费黑科技,微乐贵阳捉鸡麻将辅助(开挂)总是是有挂(2026)该软件可以轻松...
插件辅助挂!微乐小程序免费黑科... 插件辅助挂!微乐小程序免费黑科技,微乐小程序游戏破解器下载(透视)竟然真的有挂(哔哩哔哩)1、每一步...
第7分钟神器!微乐小程序免费黑... 您好,微乐小程序免费黑科技这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很...
透视游戏!微乐小程序免费黑科技... 透视游戏!微乐小程序免费黑科技,微乐小程游戏破解器下载(透视)其实真的是有挂(哔哩哔哩)1、上手简单...
六分钟教程!微乐小程序黑科技,... 六分钟教程!微乐小程序黑科技,微乐家乡官方app(开挂)其实是真的挂(2026)1、许多玩家不知道微...
2026版规律!微乐小程序免费... 2026版规律!微乐小程序免费黑科技,广西微乐小程序辅助器(透视)一贯存在有挂(哔哩哔哩)1、该软件...
3分钟辅助!微乐小程序免费黑科... 3分钟辅助!微乐小程序免费黑科技,微信小程序微乐安徽辅助器(开挂)都是真的有挂(2026)暗藏猫腻,...