我有兩個專案,一個是 Web,另一個是類別庫。我需要將物體框架放在類別庫專案中。但是,在搭建資料庫時,我無法讀取連接字串。
- 初始化秘密:
dotnet user-secrets init - 設定要存盤的專案:
dotnet user-secrets set "ConnectionStrings:MyConnString" "string" - 腳手架資料庫:
dotnet ef dbcontext scaffold Name=ConnectionStrings:MyConnString
這顯然是行不通的,因為靜態配置方法是在 web 專案中,而不是在 Class Library 專案中。
錯誤:
A named connection string was used, but the name 'ConnectionStrings:APIConnection' was not found in the application's configuration.
我可以完成這項作業的唯一方法是對連接字串進行硬編碼。
還有其他方法可以使這項作業嗎?
uj5u.com熱心網友回復:
它適用于 ASP.NET Core 6 和 EfCore v6.0.1。
這是我已經完成的步驟:
- 創建 ASP.NET Core Web API 和類別庫專案
-- > WebApp.Api\
|
> WebApp.Data\
|
> WebApp.sln
- 為 設定連接字串秘密
WebApp.Api。確保將UserSecretsId其添加到 csprojPropertyGroup:
> dotnet user-secrets init
> dotnet user-secrets set "ConnectionStrings:MyConnString" "<conn-string>"
- 安裝
Microsoft.EntityFrameworkCore并Microsoft.EntityFrameworkCore.SqlServer進入WebApp.Data. 將專案參考添加WebApp.Api到WebApp.Data. Microsoft.EntityFrameworkCore.Design為WebApp.Api專案安裝nuget 。- 在 cmd 中導航到
WebApp.Api專案檔案夾并運行以下命令:
> dotnet ef dbcontext scaffold Name=ConnectionStrings:MyConnString "Microsoft.EntityFrameworkCore.SqlServer" -p ..\WepApp.Data\
在這種情況下,scaffold命令啟動專案是WebApp.Api,目標專案是WebApp.Data
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/407935.html
標籤:
