我使用的是.NET 6.0,在项目中我有 appsettings.json 文件,其中包含以下行:
"ConnectionStrings": {
"SqlConnection": "Server=.\\SQLEXPRESS;Database=XXX;Trusted_Connection=True;MultipleActiveResultSets=true"
},
在 Azure 中,我的连接字符串定义如下:[ ] 1
当我检查 xxx.scm.azurewebsites.net/wwwroot 中的 appsettings.json 时,我可以看到它仍然使用在我的项目的 appsetting.json 中定义的 ConnectionString,但不在 Azure 中定义。
我的数据库上下文:
services.AddDbContext<MyContext>(
options => options.UseSqlServer(Configuration.GetConnectionString("SqlConnection")));
如何在 Azure 中覆盖我的连接字符串并使用它?谢谢。
最佳答案
应用服务不会更改您的 appsettings.json 文件。除了 appsettings.json 文件之外,它还会将任何应用设置(包括连接字符串)注入(inject)到您的应用程序中(作为 ENV 变量)。
关于Azure 连接字符串不会覆盖 appsettings.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74657480/