The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet, function, script file, or operable program

后端 未结 10 482
深忆病人
深忆病人 2020-12-13 22:50

When trying to scaffold with asp.net core this command

scaffold-dbcontext \"Data Source=(local);Initial Catalog=MyDb;Integrated Security=True;\" M

相关标签:
10条回答
  • 2020-12-13 23:26

    For me... when copy pasting the command from the microsoft docs, for some reason extra spacing was added around the hyphens.

    Removing the hyphens fixed it:

    wrong:

    Scaffold - DbContext "Server=(localdb)\mssqllocaldb;Database=myDbName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer - OutputDir Models
    

    good:

    Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=myDbName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
    

    The docs also say if you receive this error, try restarting Visual Studio.

    https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

    0 讨论(0)
  • 2020-12-13 23:28

    Had the same problem. In my case i was missing some dependencies, so make sure that you have the following one :

    • Microsoft.EntityFrameworkCore
    • Microsoft.EntityFrameworkCore.SqlServer
    • Microsoft.EntityFrameworkCore.Tools

    Hope this would help. :)

    0 讨论(0)
  • 2020-12-13 23:29
    1. Make sure that this is available in your project.json file "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-final".

    1. Run the command in the package manager console

    that's all it will work

    0 讨论(0)
  • 2020-12-13 23:33

    For me apparently it worked once I have also ran in Package Manager console :

     Install-Package Microsoft.EntityFrameworkCore.Tools 
    

    Also make sure :

    • To have other dependencies (for example Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.SqlServer.Design...) referenced depending of your needs.

    • To select the right assembly as target for your commands in the top-right corner of the PM console (I am frequently fooled by forgetting it...)

    Another problem I encountered : with the dbcontext located in a separate class library, I was encountering the following error :

    Unable to find provider assembly with name Microsoft.EntityFrameworkCore.SqlServer. Ensure the specified name is correct and is referenced by the project.

    Which I was able to fix by setting my class library as Startup project in VS (don't ask why as it seems meaningless, but it worked).

    Late edit, there's something else to know : You can't run Scaffold-DbContext against a class library targetting only .Net Standard, you must also enable netcoreapp in it, or Scaffold-DbContext will complain. To support both targets, edit the csproj to put : <TargetFrameworks>netcoreapp2.2;netstandard2.0</TargetFrameworks> Instead of <TargetFramework> section.

    After all these you'll be able to run your Scaffold-DbContext command line with proper arguments and connection string.

    0 讨论(0)
  • 2020-12-13 23:35

    Make sure you are using the right console, the "Package Manager Console". There is also a "Terminal" console which looks very similar, but doesn't work for this command. Package Manager Console can be found in View -> Other Windows (as of Visual Studio 2019, ver. 16.6.5)

    0 讨论(0)
  • 2020-12-13 23:36

    If you're using .NetCore 2.2 the command below works like a charm for me on either command prompt or Git Bash. Make sure that you are directly on the project folder before running the command.

    For example C:\App\ProjectName:

     dotnet ef dbcontext scaffold "Server=.\;Database=Databasename;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Model
    
    0 讨论(0)
提交回复
热议问题