您的位置:首页 > 数据库

net core生成数据库,使用Scaffold-DbContext报错“‘Scaffold-DbContext’ is not recognized as the name of a cmdlet”

2017-03-02 14:46 507 查看
win7,64位,vs2015

net core生成数据库,使用Scaffold-DbContext报错“‘Scaffold-DbContext’ is not recognized as the name of a cmdlet”

学习的生成数据库信息的文章来自:http://www.cnblogs.com/baobaodong/p/5871357.html

运行的代码是:PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles



就在网上搜索下,找到一篇:http://www.genshuixue.com/i-cxy/p/12865486



之后我吧代码加上,再次运行 Scaffold-DbContext 



提示我powershell版本需要升级,我就从网上下载一个powershell3进行安装,重新启动电脑,但是还是不行,看到如下错误代码

下面看到的错误信息,有看到包名就进行安装下

PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles

System.AggregateException: One or more errors occurred. ---> 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.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies.
系统锟揭诧拷锟斤拷指锟斤拷锟斤拷锟侥硷拷锟斤拷    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean
forIntrospection, Boolea n suppressSecurityChecks)

   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean
suppressSecurityChecks)

   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.DatabaseOperations.ReverseEngineerAsync(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overw
riteFiles, CancellationToken cancellationToken)

    at Microsoft.EntityFrameworkCore.Design.Internal.OperationExecutor.ReverseEngineerAsync(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, Boolean useDataAnnota
tions, Boolean overwriteFiles, CancellationToken cancellationToken)

   at Microsoft.EntityFrameworkCore.Tools.Cli.DbContextScaffoldCommand.<ExecuteAsync>d__1.MoveNext()

   --- End of inner exception stack trace ---

    at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)

    at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) 

   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)

    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args) 

 ---> (Inner Exception #0) 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 th
e project. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.EntityFrameworkCore.SqlServer.Design, Version=1.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. 系统锟揭诧拷锟斤拷指锟斤拷锟斤拷锟侥硷拷锟斤拷

    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolea
n suppressSecurityChecks)

   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean
suppressSecurityChecks)

    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.DatabaseOperations.ReverseEngineerAsync(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overw
riteFiles, CancellationToken cancellationToken)

    at Microsoft.EntityFrameworkCore.Design.Internal.OperationExecutor.ReverseEngineerAsync(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, Boolean useDataAnnota
tions, Boolean overwriteFiles, CancellationToken cancellationToken)

    at Microsoft.EntityFrameworkCore.Tools.Cli.DbContextScaffoldCommand.<ExecuteAsync>d__1.MoveNext()<--- 

 One or more errors occurred. 

PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles

C:\Users\Administrator\Desktop\test7\src\test7\project.json(21,54): warning NU1012: Dependency conflict. Microsoft.EntityFrameworkCore.SqlServer.Design 1.1.0 expected Microsoft.EntityFrameworkCore.SqlServer >= 1.1.0 but received 1.0.1 C:\Users\Administrator\Desktop\test7\src\test7\project.json(24,43):
warning NU1012: Dependency conflict. Microsoft.EntityFrameworkCore 1.1.0 expected Microsoft.Extensions.Logging >= 1.1.0 but received 1.0.0 

 C:\Users\Administrator\Desktop\test7\src\test7\Modles\FirstAppDemoDbContext.cs(14,32): warning CS0114: 'FirstAppDemoDbContext.OnConfiguring(DbContextOptionsBuilder)' hides inherited member 'DbContext.OnConfiguring(DbContextOptionsBuilder)'. To make the cur
rent member override that implementation, add the override keyword. Otherwise add the new keyword. 

 C:\Users\Administrator\Desktop\test7\src\test7\Startup.cs(69,30): warning CS0168: The variable 'ex' is declared but never used 

 Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest def
inition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)

PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles

C:\Users\Administrator\Desktop\test7\src\test7\project.json(21,54): warning NU1012: Dependency conflict. Microsoft.EntityFrameworkCore.SqlServer.Design 1.1.0 expected Microsoft.EntityFrameworkCore.SqlServer >= 1.1.0 but received 1.0.1 C:\Users\Administrator\Desktop\test7\src\test7\Modles\FirstAppDemoDbContext.cs(14,32):
warning CS0114: 'FirstAppDemoDbContext.OnConfiguring(DbContextOptionsBuilder)' hides inherited member 'DbContext.OnConfiguring(DbContextOptionsBuilder)'. To make the cur rent member override that implementation, add the override keyword. Otherwise add the
new keyword. 

 C:\Users\Administrator\Desktop\test7\src\test7\Startup.cs(69,30): warning CS0168: The variable 'ex' is declared but never used 

 Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest def
inition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)

PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles

C:\Users\Administrator\Desktop\test7\src\test7\Modles\FirstAppDemoDbContext.cs(14,32): warning CS0114: 'FirstAppDemoDbContext.OnConfiguring(DbContextOptionsBuilder)' hides inherited member 'DbContext.OnConfiguring(DbContextOptionsBuilder)'. To make the current
member override that implementation, add the override keyword. Otherwise add the new keyword. C:\Users\Administrator\Desktop\test7\src\test7\Startup.cs(69,30): warning CS0168: The variable 'ex' is declared but never used 

 Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest def
inition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)

PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles

C:\Users\Administrator\Desktop\test7\src\test7\Modles\FirstAppDemoDbContext.cs(14,32): warning CS0114: 'FirstAppDemoDbContext.OnConfiguring(DbContextOptionsBuilder)' hides inherited member 'DbContext.OnConfiguring(DbContextOptionsBuilder)'. To make the current
member override that implementation, add the override keyword. Otherwise add the new keyword. C:\Users\Administrator\Desktop\test7\src\test7\Startup.cs(69,30): warning CS0168: The variable 'ex' is declared but never used 

 Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest def
inition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)

PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles

C:\Users\Administrator\Desktop\test7\src\test7\Startup.cs(69,30): warning CS0168: The variable 'ex' is declared but never used Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest def inition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)

PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition
does not match the assembly reference. (Exception from HRESULT: 0x80131040)    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args) 

PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition
does not match the assembly reference. (Exception from HRESULT: 0x80131040)    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)

之前上面看到的错误信息,有看到包名就进行安装下

PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles

System.InvalidOperationException: The following file(s) already exist in directory C:\Users\Administrator\Desktop\test7\src\test7\Modles: Employee.cs. Use the Force flag to overwrite these files.

   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineeringGenerator.CheckOutputFiles(String outputPath, String dbContextClassName, IModel metadataModel, Boolean overwriteFiles)

   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineeringGenerator.GenerateAsync(ReverseEngineeringConfiguration configuration, CancellationToken cancellationToken)

   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.Linq.Buffer`1..ctor(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)

The following file(s) already exist in directory C:\Users\Administrator\Desktop\test7\src\test7\Modles: Employee.cs. Use the Force flag to overwrite these files.

PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles

System.InvalidOperationException: The following file(s) already exist in directory C:\Users\Administrator\Desktop\test7\src\test7\Modles: Employee.cs. Use the Force flag to overwrite these files.

   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineeringGenerator.CheckOutputFiles(String outputPath, String dbContextClassName, IModel metadataModel, Boolean overwriteFiles)

   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineeringGenerator.GenerateAsync(ReverseEngineeringConfiguration configuration, CancellationToken cancellationToken)

   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.Linq.Buffer`1..ctor(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)
The following file(s) already exist in directory C:\Users\Administrator\Desktop\test7\src\test7\Modles: Employee.cs. Use the Force flag to overwrite these files.

如上说的问题是当前数据库已经存在model里面的类的表了,所以我就改了下数据库里的表的名称,如下就执行成功了

PM> Scaffold-DbContext  "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles
PM> 



如果对ASP.NET Core包进行升级的话,版本从1.0至1.1,或其他的包进行了更新之后报错

Scaffold-DbContext : The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
was included, verify that th

e path is correct and try again.

At line:1 char:1

+ Scaffold-DbContext "Data Source=.;Initial Catalog=testData;User ID=sa;Passw ...

+ ~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (Scaffold-DbContext:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

这样就对“Microsoft.EntityFrameCore”相关的包进行更新下就可以了

如果提示连接数据库语句的问题,看看是不是有安装多个sqlserver版本的数据库,进行区别下就可以了。

如果有找到文档说是需要升级下"Microsoft.EntityFrameworkCore.Tools.DotNet":"1.0.0-preview3-final",安装这个包的时候就会提示此包不支持当前的nuget,这个是在vs2017下应该可以,这个是个人猜测和看文档说的,没有具体实践。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐