[英]Why does the EF Scaffold-DbContext command throw an NRE, after reporting 'No design-time services were found.', when targeting net6.0?
我目前使用的是最新的 net6.0 預覽版: dotnet-sdk-6.0.100-preview.7.21379.14-win-x64
我必須重新定位到 net5.0 並回滾到使用其關聯的包才能使 Scaffold-DbContext 工作。
完整的 EF 命令如下:
Scaffold-DbContext Name=<connection-string-name> Microsoft.EntityFrameworkCore.SqlServer -Context MyDbContext -OutputDir ModelsNew -NoPluralize -Force -Verbose
這是最后的消息,然后是 NRE 的堆棧跟蹤:
No design-time services were found.
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.EntityFrameworkCore.Storage.Internal.NamedConnectionStringResolver.get_ApplicationServiceProvider() in Microsoft.EntityFrameworkCore.Relational.dll:token 0x6000638+0x0
at Microsoft.EntityFrameworkCore.Storage.Internal.NamedConnectionStringResolverBase.ResolveConnectionString(String connectionString) in Microsoft.EntityFrameworkCore.Relational.dll:token 0x600063a+0xc
at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions) in Microsoft.EntityFrameworkCore.Design.dll:token 0x600023d+0x79
at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize) in Microsoft.EntityFrameworkCore.Design.dll:token 0x600043e+0x11c
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluarlize) in Microsoft.EntityFrameworkCore.Design.dll:token 0x60003ed+0x32
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_0.<.ctor>b__0() in Microsoft.EntityFrameworkCore.Design.dll:token 0x60006ed+0x0
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0() in Microsoft.EntityFrameworkCore.Design.dll:token 0x60006f3+0x0
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) in Microsoft.EntityFrameworkCore.Design.dll:token 0x600066a+0xc
Object reference not set to an instance of an object.
項目均指定LangVersion預覽
升級到版本 6.0.100-rc.1.21458.32 、.NET 6.0.0-rc.1 候選版本和 dotnet-ef版本 6.0.0-rc.1.21452.10后,此 Scaffold-DbContext 崩潰仍然存在工具。
解決方法是簡單地用顯式連接字符串替換命名連接字符串。 執行此操作時,將報告以下警告:
To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
Scaffold-DbContext 命令似乎已經完成了它的工作; 但是盡管包含了 -Verbose 開關,但 NuGet PMC 窗口中沒有出現任何輸出。
此問題已在 VS 2022 版本 17.1.0 預覽版 1.1 的正式版本中得到解決。
EF Scaffold-DbContext 命令現在可以使用命名連接字符串成功完成,同時面向 net6.0。
即使在發布候選版本期間,這也被打破了; 但在最后一次發射時被及時修復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.