为啥 EF Scaffold-DbContext 命令在针对 net6.0 报告“未找到设计时服务”后会引发 NRE?

Posted

技术标签:

【中文标题】为啥 EF Scaffold-DbContext 命令在针对 net6.0 报告“未找到设计时服务”后会引发 NRE?【英文标题】:Why does the EF Scaffold-DbContext command throw an NRE, after reporting 'No design-time services were found.', when targeting net6.0?为什么 EF Scaffold-DbContext 命令在针对 net6.0 报告“未找到设计时服务”后会引发 NRE? 【发布时间】:2021-09-02 19:35:14 【问题描述】:

我目前使用的是最新的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 preview

升级到版本 6.0.100-rc.1.21458.32、.NET 6.0.0-rc.1 候选版本和版本 6.0 后,此 Scaffold-DbContext 崩溃仍然存在.0-rc.1.21452.10 的 dotnet-ef 工具。

【问题讨论】:

您是否安装了 EF Core 6 工具? 感谢您的提醒。实际上,我之前曾尝试过以下命令:dotnet tool update --global dotnet-ef,但这只会安装最新的已发布版本 5.0.9 的工具。根据您的建议,我进一步搜索并发出以下命令:dotnet tool install --global dotnet-ef --version 6.0.0-preview.7.21378.4 不幸的是,此更新无法解决上面报告的空引用异常。我重新启动了 Windows。 我已通过以下命令升级了 dotnet-ef 工具:dotnet tool update --global dotnet-ef --version 6.0.0-rc.1.21452.10,但行为没有明显变化。 如果你在乎,你并不孤单。我有同样的问题。我还求助于在命令中包含连接字符串。希望这将很快得到解决。 ef 工具尝试使用您的CreateHostBuilder 来发现数据库上下文。但是我同意在这种情况下可以改进错误处理。您如何创建一个问题github.com/dotnet/efcore 【参考方案1】:

解决方法只是将命名连接字符串替换为显式连接字符串。执行此操作时,将报告以下警告:

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 窗口中未显示任何输出。

【讨论】:

【参考方案2】:

此问题已在 VS 2022 的正式版本 17.1.0 预览版 1.1 中得到解决,很可能是由于 dotnet-ef 版本 6.0.0 的发布。

EF Scaffold-DbContext 命令现在使用命名连接字符串成功完成,同时针对 net6.0。

即使在发布候选版本期间,这也被破坏了;但在最终发布前及时修复。

【讨论】:

以上是关于为啥 EF Scaffold-DbContext 命令在针对 net6.0 报告“未找到设计时服务”后会引发 NRE?的主要内容,如果未能解决你的问题,请参考以下文章

EF Core 入门

EF Core Scaffold DbContext

EF.Core 读取数据库创建实体类

EF Core DB first - 我需要索引定义吗?

EF Core 2.0中如何手动映射数据库的视图为实体

Scaffold-DbContext:找不到命令