Efcore: EF Core 2.0 Scaffold-DbContext null reference

Created on 3 Oct 2017  路  10Comments  路  Source: dotnet/efcore

Hi I am trying to generate model form existing DB in ASP.NET Core 2.0 project with EF 2.0 tooling. After running command below I am getting null reference exception.
Scaffold-DbContext "Server=tcp:mysqlserveraddress,1433;Initial Catalog=mydb;Persist Security Info=False;User ID=user;Password=pass;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

I was following these instructions, while I have used 2.0 tooling: https://ef.readthedocs.io/en/staging/platforms/aspnetcore/existing-db.html

Exception message: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. 
Stack trace:
---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext..ctor(OperationExecutor executor, Object resultHandler, IDictionary args)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at Microsoft.EntityFrameworkCore.Tools.AppDomainOperationExecutor.Execute(String operationName, Object resultHandler, IDictionary arguments)
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid4[T0,T1,T2,T3](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase.InvokeOperationImpl(String operationName, IDictionary arguments)
   at Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase.ScaffoldContext(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles)
   at Microsoft.EntityFrameworkCore.Tools.Commands.DbContextScaffoldCommand.Execute()
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)

Further technical details

EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows
IDE: Visual Studio 2017 Preview 2

Thank you for help in advance.

closed-question

Most helpful comment

Try my "EF Core Power Tools" 馃槃

All 10 comments

Try my "EF Core Power Tools" 馃槃

@ErikEJ thank you. It worked like a charm! Kudos! From now on I will be using Power Tools for such tasks. Nevertheless I would be interested what may be causing issue

Exactly the same issue here, any workaround?

I had the same issue when trying to execute Scaffold-DbContext command. Adding explicit table names after -Tables fixed the issue.

We are having the same issue here since upgrading to 2.0. Any updates on this? Explicitly naming tables works but is a huge hassle and hard to maintain across the team.

In our particular instance, we migrated recently from dotnet 1.x to 2.0 and missed a CLI tool reference.

Our CLI Tool Reference for Microsoft.EntityFrameworkCore.Tools.DotNet was targeting version 1.0.0 while our EF Core packages were all targeting 2.0+. Updating the version of the CLI tool to version 2.0.0 and performing a dotnet restore resolved our issue.

Restarting Visual Studio solved my problem. Took me half an hour before I tried this known fixer of strange vs problems ;-)

This is caused by a mismatch between tool (1.x) and runtime (2.x) versions. There were intentional breaking changes made between these versions. This restriction may be relaxed in the next major version since breaking changes are less likely in this area of the code.

Note, the opposite issue (using a 2.0 tool on a 1.x runtime) was discussed in #9984

Try my "EF Core Power Tools" 馃槃

Quick and pratice

Was this page helpful?
0 / 5 - 0 ratings