Efcore: Scaffold-DbContext command not working

Created on 11 Apr 2017  路  7Comments  路  Source: dotnet/efcore

When running the command to generate a model for an existing DB:
Scaffold-DbContext "Server=MyIpAddress;Database=MyDb;User Id=myUid;Password=myPw;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -verbose

I am receiving an error stating that I need to reference Microsoft.EntityFrameworkCore.SqlServer.Design however, it is referenced in my .csproj file as both a DotNetCliToolReference and a PackageReference.

Package References

ERROR MESSAGE:
Unable to find design-time provider assembly. Please install the Microsoft.EntityFrameworkCore.SqlServer.Design NuGet package and ensure that the package is referenced by the project.

STACK TRACE:
Using project 'Mpi.Api.Aptify'.
Using startup project 'Mpi.Auth.Server'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile "C:\src\work\MPI Web\Mpi.Auth\Mpi.Auth.Server\bin\Debug\netcoreapp1.1\Mpi.Auth.Server.deps.json" --additionalprobingpath C:\Users\lfuller\.nuget\packages --runtimeconfig "C:\src\work\MPI Web\Mpi.Auth\Mpi.Auth.Server\bin\Debug\netcoreapp1.1\Mpi.Auth.Server.runtimeconfig.json" C:\Users\lfuller\.nuget\packages\microsoft.entityframeworkcore.tools\1.1.0\tools\netcoreapp1.0\ef.dll dbcontext scaffold "Server=10.128.3.25;Database=Aptify;User Id=sa;Password=H-funTR2fND+fbPe;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer --json --output-dir Models --verbose --no-color --prefix-output --assembly "C:\src\work\MPI Web\Mpi.Auth\Mpi.Auth.Server\bin\Debug\netcoreapp1.1\Mpi.Api.Aptify.dll" --startup-assembly "C:\src\work\MPI Web\Mpi.Auth\Mpi.Auth.Server\bin\Debug\netcoreapp1.1\Mpi.Auth.Server.dll" --project-dir "C:\src\work\MPI Web\Mpi.Auth\Mpi.Api.Aptify" --content-root "C:\src\work\MPI Web\Mpi.Auth\Mpi.Auth.Server" --data-dir "C:\src\work\MPI Web\Mpi.Auth\Mpi.Auth.Server\bin\Debug\netcoreapp1.1\\" --root-namespace Mpi.Api.Aptify
Using assembly 'Mpi.Api.Aptify'.
Using startup assembly 'Mpi.Auth.Server'.
Using application base 'C:\src\work\MPI Web\Mpi.Auth\Mpi.Auth.Server\bin\Debug\netcoreapp1.1'.
Using content root 'C:\src\work\MPI Web\Mpi.Auth\Mpi.Auth.Server'.
Using root namespace 'Mpi.Api.Aptify'.
Using project directory 'C:\src\work\MPI Web\Mpi.Auth\Mpi.Api.Aptify'.
Using data directory 'C:\src\work\MPI Web\Mpi.Auth\Mpi.Auth.Server\bin\Debug\netcoreapp1.1\'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to find design-time provider assembly. Please install the Microsoft.EntityFrameworkCore.SqlServer.Design NuGet package and ensure that the package is referenced by the project. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.EntityFrameworkCore.SqlServer.Design, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks, IntPtr ptrLoadContextBinder)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks, IntPtr ptrLoadContextBinder)
   at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
   at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.GetProviderDesignTimeServices(String provider, Boolean throwOnError)
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.GetProviderDesignTimeServices(String provider, Boolean throwOnError)
   at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.Build(String provider)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContextAsync(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.<ScaffoldContextImpl>d__22.MoveNext()
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source, Int32& length)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass4_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to find design-time provider assembly. Please install the Microsoft.EntityFrameworkCore.SqlServer.Design NuGet package and ensure that the package is referenced by the project.

Steps to reproduce

NOTE: I copied the project to a separate directory away from the rest of the assembly and was able to run the command successfully, however the command still cannot be ran on the project in the primary solution. I tried deleting the Packages folder along with the bin and obj with no luck.
What about being part of a solution could affect the project's ability to resolve dependencies?

I'm unable to post the project to GitHub at the moment.

Further technical details

EF Core version: (found in project.json or packages.config)
Database Provider: 1.1.1
Operating system: Windows 10
IDE: Visual Studio 2017

closed-question

Most helpful comment

@ChuckkNorris You could try my GUI tool, it avoids adding design components to your production code all together https://twitter.com/ErikEJ/status/834405829889638402

All 7 comments

@ChuckkNorris You could try my GUI tool, it avoids adding design components to your production code all together https://twitter.com/ErikEJ/status/834405829889638402

Waiting to get input from @bricelam on this one.

@ajcvickers Thanks for the heads up, sir

@ChuckkNorris You are using Mpi.Auth.Server as your start-up project when running Scaffold-DbContext. Is this intentional? If not, you probably intended to use -StartupProject Mpi.Api.Aptify.

Thanks @bricelam - that resolved it. I assumed running the command in the Mpi.Api.Aptify project directory would tell it to use that project.

I had a similar problem which was being caused by VS 2017. Despite having the Default project: set to the correct project in the Package Manager Console, it was still insisting on trying to use the project set in VS 2017 as the startup project. Once I changed the startup project to the project I was trying Scaffold-DbContext against, the Scaffold-DbContext command worked as expected.

We're going to try and address some of the usability issue in #8354.

There are two projects involved every time you run EF commands:

  • Target/Default/DbContext/Migrations Project--The project containing your Migration classes. (e.g. a class library)
  • Startup Project--The application we attempt to emulate (working directory, configuration, dependencies, etc.) at design-time. (e.g. an ASP.NET Core web application)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

rowanmiller picture rowanmiller  路  101Comments

obelixA picture obelixA  路  105Comments

divega picture divega  路  100Comments

satyajit-behera picture satyajit-behera  路  275Comments

0xdeafcafe picture 0xdeafcafe  路  189Comments