![](/img/trans.png)
[英]How to retrieve connection string in class from .NET Core 2.1 Web API w/o Entity Framework
[英]Deploying Web API to Azure - Entity Framework Core migrations connection string not being picked up
我正在嘗試使用 Visual Studio 2022 將我的 Web API 部署到 Azure App Service,但是當我嘗試應用遷移時,雖然它發現了數據庫上下文,但它並沒有發現連接字符串:
這是我的數據庫上下文類的代碼:
namespace MyPortfolioWebAPI.Data
{
public class MyPortfolioContext:DbContext
{
public MyPortfolioContext(DbContextOptions<MyPortfolioContext> options) : base(options)
{
}
public DbSet<Emails> Emails { get; set; } = null!;
public DbSet<Projects> Projects { get; set; } = null!;
}
}
這是我的appsettings.json
文件:
它在我的本地機器上運行良好,但是我正在嘗試將它發布到 Azure。
更改您的appsettings.json
文件,如下所示。 這個問題應該得到解決。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Your_Connection_String"
},
"AllowedHosts": "*"
}
我在本地測試並使用 .net6 項目。 並且發現builder.Configuration.GetConnectionString
可以直接識別ConnectionStrings
鍵下的元素。
您的代碼還有一層Data nested
,這就是它無法讀取的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.