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

环境

win7,64位,vs2015

目的

asp.net core 使用Scaffold-DbContext 生成 DbContext 文件

问题

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 ModlesSystem.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, IEnumerable1 schemas, IEnumerable1 tables, Boolean useDataAnnotations, Boolean overw riteFiles, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Design.Internal.OperationExecutor.ReverseEngineerAsync(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable1 schemaFilters, IEnumerable1 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.Task1.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, IEnumerable1 schemas, IEnumerable1 tables, Boolean useDataAnnotations, Boolean overw riteFiles, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Design.Internal.OperationExecutor.ReverseEngineerAsync(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable1 schemaFilters, IEnumerable1 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 ModlesC:\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 ModlesC:\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 ModlesC:\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 ModlesC:\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 ModlesC:\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 ModlesUnhandled 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 ModlesUnhandled 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 ModlesSystem.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, IEnumerable1 schemas, IEnumerable1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.<ScaffoldContextImpl>d__22.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass4_01.<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 ModlesSystem.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, IEnumerable1 schemas, IEnumerable1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.<ScaffoldContextImpl>d__22.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 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 ModlesPM>

如果对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 the 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下应该可以,这个是个人猜测和看文档说的,没有具体实践。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,717评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,501评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,311评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,417评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,500评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,538评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,557评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,310评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,759评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,065评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,233评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,909评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,548评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,172评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,420评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,103评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,098评论 2 352

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,457评论 0 23
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,646评论 18 139
  • 夜,即墨。 希望旋即遮盖。 一切变得微不足道。 静等黎明的到来, 即使昙花一现。 这样,足够。 陌上花开 2017...
    玫瑰花的梦阅读 114评论 0 1
  • 建议阅读时间:10分钟 我知道一切都不可能回去了。但是倘若有可能,我是多么希望,能让你在一个晴朗有风、小憩过没有任...
    真真徐来阅读 19,342评论 133 744
  • 写作是心灵和文字的舞蹈,是思想的一次远足,像我这样有一出没一出的简书,充其量算作阳台散步。 于我而言,写作就是流动...
    水元阅读 481评论 2 3