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版辅助挂!pokerr... 您好,腾威互娱 辅助工具这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多...
做出回应!广东闲来辅助神器(辅... 做出回应!广东闲来辅助神器(辅助)总是存在有辅助挂(详细教程)-哔哩哔哩1、实时广东闲来辅助神器透视...
突发!聚星ai辅助工具下载,樱... 突发!聚星ai辅助工具下载,樱花之盛辅助软件,演示辅助神器(竟然有挂)-哔哩哔哩1、聚星ai辅助工具...
据通报!欢聚水鱼智能辅助app... 据通报!欢聚水鱼智能辅助app(辅助)一贯真的是有辅助器(有挂方略)-哔哩哔哩该软件可以轻松地帮助玩...
黑科技技巧!哈糖大菠萝怎么开挂... 黑科技技巧!哈糖大菠萝怎么开挂,广西老友辅助,方针辅助教程(有挂存在)-哔哩哔哩1、玩家可以在哈糖大...
这一问题亟待解决!凑一桌关春天... 这一问题亟待解决!凑一桌关春天怎么才能开挂(辅助)竟然真的有辅助工具(有挂秘笈)-哔哩哔哩凑一桌关春...
这一问题亟待解决!pokemm... 这一问题亟待解决!pokemmo内置修改器,湖北卡五星技能,要领辅助软件(有挂方法)-哔哩哔哩1、许...
据悉!微乐小程序辅助开发透视(... 据悉!微乐小程序辅助开发透视(辅助)好像存在有辅助挂(有挂解惑)-哔哩哔哩1、起透看视 微乐小程序辅...
近年来!agpoker辅助,微... 近年来!agpoker辅助,微信中至有辅助吗,机巧辅助脚本(有挂助手)-哔哩哔哩该软件可以轻松地帮助...
现有关情况通报如下!中至九江辅... 现有关情况通报如下!中至九江辅助(辅助)果然真的是有辅助神器(确实有挂)-哔哩哔哩中至九江辅助透视方...