简体   繁体   中英

Why does the EF Scaffold-DbContext command throw an NRE, after reporting 'No design-time services were found.', when targeting net6.0?

I am currently using the latest net6.0 preview: dotnet-sdk-6.0.100-preview.7.21379.14-win-x64

I have to retarget to net5.0 and roll back to using its associated packages in order to get the Scaffold-DbContext to work.

The full EF Command is as follows:

Scaffold-DbContext Name=<connection-string-name> Microsoft.EntityFrameworkCore.SqlServer -Context MyDbContext -OutputDir ModelsNew -NoPluralize -Force -Verbose 

Here is the final message, followed by the stack trace for the 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.

The projects all specify LangVersion preview

This Scaffold-DbContext crash remains after upgrading to Version 6.0.100-rc.1.21458.32 , the .NET 6.0.0-rc.1 Release Candidate, and to Version 6.0.0-rc.1.21452.10 of the dotnet-ef tools.

The work-around is simply to replace the Named Connection String with the explicit Connection String. When you do this, the following warning is reported:

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.

The Scaffold-DbContext command appears to have completed its work; but no output appeared in the NuGet PMC Window, despite inclusion of the -Verbose switch.

This issue has been addressed in the official release of VS 2022, Version 17.1.0 Preview 1.1.

The EF Scaffold-DbContext command now completes successfully with Named Connection Strings while targeting net6.0.

This was broken even during the Release Candidate versions; but was fixed in time for the final launch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM