.net 当I';我是本地人

.net 当I';我是本地人,.net,asp.net-core,asp.net-core-webapi,connection-string,.net,Asp.net Core,Asp.net Core Webapi,Connection String,我在.NET 5的API中,在appsettings.json中我有以下内容: 我正在处理的项目有这样一个连接字符串 "DefaultConnection": "Data Source=someserverAWS.com; initial catalog=ProjectDb; Persist Security Info=True; User Id=lorenz; Password=54321 ; MultipleActiveResultSets=True"

我在.NET 5的API中,在appsettings.json中我有以下内容:

我正在处理的项目有这样一个连接字符串

"DefaultConnection": "Data Source=someserverAWS.com; initial catalog=ProjectDb; Persist Security Info=True; User Id=lorenz; Password=54321 ; MultipleActiveResultSets=True"
"DefaultConnection": "Data Source=localhost;initial catalog=ProjectDb;Persist Security Info=True;User Id=sa;Password=12345;MultipleActiveResultSets=True" 
但我没有访问该服务器的权限,因此为了使代码在本地为我工作,我必须每次(例如,我正在开发一个新功能)都将其更改为类似这样的内容

"DefaultConnection": "Data Source=someserverAWS.com; initial catalog=ProjectDb; Persist Security Info=True; User Id=lorenz; Password=54321 ; MultipleActiveResultSets=True"
"DefaultConnection": "Data Source=localhost;initial catalog=ProjectDb;Persist Security Info=True;User Id=sa;Password=12345;MultipleActiveResultSets=True" 

两者都是不同的,所以问题是如何在我的本地上有一些东西,当我从repo中提取时,这些东西会持续存在,但在推送时不会更改repo文件?

为您拥有的每个环境创建不同的
appsettings.json

appsettings.Development.json
将在调试中自动使用,并且
appsettings.Production.json
将在生产中自动使用


appsettings.json
是任何特定环境文件中未覆盖的属性以及没有匹配文件的环境的默认回退文件

这很简单。两步

第一步。设置环境变量:
ASPNETCORE\u环境
您可以通过Windows环境变量设置添加此项

ASPNETCORE\u环境设置为
Development

或者如果通常情况下,
launchSettings.json
文件将在
/Properties
文件夹下创建。如果您有该文件,可以检查它是否自动设置了环境变量

第二步。将您的
appsettings.json
复制到
appsettings.Development.json
现在,您的应用程序将使用
appsettings.Development.json
覆盖默认的
appsettings.json
文件

您可以在
appsettings.Development.json
文件中添加本地调试值

现在您可以编辑您的连接字符串


我强烈建议您不要让git跟踪开发文件。

除了配置
appsettings.{ENVIRONMENT\u NAME}.json之外,您还可以将其用于本地环境。这给git带来了一些问题,但最终还是成功了,谢谢