为什么在针对 net6.0 时,EF Scaffold-DbContext 命令在报告 'No design-time services were found.' 后抛出 NRE?

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 preview

在升级到 版本 6.0.100-rc.1.21458.32、.NET 6.0.0-rc.1 候选发布版以及dotnet-ef 工具版本 6.0.0-rc.1.21452.10

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

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 命令似乎已完成其工作;但是 NuGet PMC Window 中没有输出,尽管包含了 -Verbose 开关。

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

EF Scaffold-DbContext 命令现在可以在以 net6.0 为目标时使用命名连接字符串成功完成。

这在发布候选版本中甚至被打破了;但在最终发布时及时修复。