[英]EF Core2.2: Scaffold-DbContext not working with named connection string in WPF project
我想在我的.net Framework 4.7項目中首先使用EF Core 2.2和Database。 它可以正常運行此命令:
Scaffold-DbContext "data source=dbServer;initial catalog=myData;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Database
但隨后MS發出警告:
警告要保護連接字符串中可能存在的敏感信息,應將其移出源代碼。 有關存儲連接字符串的指導,請參閱http://go.microsoft.com/fwlink/?LinkId=723263 。
所以我認為我按照說明執行並更新腳本如下:
Scaffold-DbContext "name=MyConnection" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Database
這會導致此錯誤:
使用了命名連接字符串,但在應用程序的配置中找不到名稱“MyConnection”。 請注意,只有在使用“IConfiguration”和服務提供程序時才支持命名連接字符串,例如在典型的ASP.NET Core應用程序中。 有關詳細信息,請參閱https://go.microsoft.com/fwlink/?linkid=850912 。
命名連接本身工作正常,我已經通過命令EF核心生成的第一行並使用第二行測試了它:
//optionsBuilder.UseSqlServer("data source=dbServer;initial catalog=myData;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework");
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);
啟動程序,它可以按預期連接和顯示數據。 但我更願意在Scaffold-DbContext中設置alrady。 有任何想法嗎?
為了完整起見,這里是app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="MyConnection" connectionString="data source=dbServer;initial catalog=myData;integrated security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
...
EF Core2.2:Scaffold-DbContext無法在WPF項目中使用命名連接字符串
事實。 這兩個錯誤消息
請注意,只有在使用
IConfiguration
和服務提供程序時才支持命名連接字符串,例如在典型的ASP.NET Core應用程序中。
和腳手架的DbContext用於命令文檔-Connection
參數
...對於ASP.NET Core 2.x項目,值可以是name = <連接字符串的名稱> 。 在這種情況下,名稱來自為項目設置的配置源...
表示命名連接字符串不能與基於“遺留” app.config
(NET Framework)的項目中的Scaffold-DbContext
命令一起使用。
因此,對於Scaffold-DbContext
您必須使用實際的連接字符串。 並在這里命名連接字符串(我猜是警告):
optionsBuilder.UseSqlServer(
ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.