net core生成數(shù)據(jù)庫(kù),使用Scaffold-DbContext報(bào)錯(cuò)“‘Scaffold-DbContext’ is not recognized as the name of a cmdlet”

環(huán)境

win7,64位,vs2015

目的

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

問(wèn)題

net core生成數(shù)據(jù)庫(kù),使用Scaffold-DbContext報(bào)錯(cuò)“‘Scaffold-DbContext’ is not recognized as the name of a cmdlet”
學(xué)習(xí)的生成數(shù)據(jù)庫(kù)信息的文章來(lái)自:http://www.cnblogs.com/baobaodong/p/5871357.html

生成的過(guò)程

運(yùn)行的代碼是:PM> Scaffold-DbContext "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Modles
運(yùn)行結(jié)果如下:

就在網(wǎng)上搜索下,找到一篇:http://www.genshuixue.com/i-cxy/p/12865486


之后我吧代碼加上,再次運(yùn)行 Scaffold-DbContext

提示我powershell版本需要升級(jí),我就從網(wǎng)上下載一個(gè)powershell3進(jìn)行安裝,重新啟動(dòng)電腦,但是還是不行,看到如下錯(cuò)誤代碼
下面看到的錯(cuò)誤信息,有看到包名就進(jìn)行安裝下
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. 系統(tǒng)錕揭詫拷錕斤拷指錕斤拷錕斤拷錕僥鹼拷錕斤拷 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. 系統(tǒng)錕揭詫拷錕斤拷指錕斤拷錕斤拷錕僥鹼拷錕斤拷 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) 之前上面看到的錯(cuò)誤信息,有看到包名就進(jìn)行安裝下 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.
如上說(shuō)的問(wèn)題是當(dāng)前數(shù)據(jù)庫(kù)已經(jīng)存在model里面的類(lèi)的表了,所以我就改了下數(shù)據(jù)庫(kù)里的表的名稱(chēng),如下就執(zhí)行成功了
PM> Scaffold-DbContext "Data Source=.;Initial Catalog=test;User ID=sa;Password=sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir ModlesPM>

如果對(duì)ASP.NET Core包進(jìn)行升級(jí)的話(huà),版本從1.0至1.1,或其他的包進(jìn)行了更新之后報(bào)錯(cuò)
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

這樣就對(duì)“Microsoft.EntityFrameCore”相關(guān)的包進(jìn)行更新下就可以了

如果提示連接數(shù)據(jù)庫(kù)語(yǔ)句的問(wèn)題,看看是不是有安裝多個(gè)sqlserver版本的數(shù)據(jù)庫(kù),進(jìn)行區(qū)別下就可以了。

如果有找到文檔說(shuō)是需要升級(jí)下"Microsoft.EntityFrameworkCore.Tools.DotNet":"1.0.0-preview3-final",安裝這個(gè)包的時(shí)候就會(huì)提示此包不支持當(dāng)前的nuget,這個(gè)是在vs2017下應(yīng)該可以,這個(gè)是個(gè)人猜測(cè)和看文檔說(shuō)的,沒(méi)有具體實(shí)踐。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,890評(píng)論 0 23
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,923評(píng)論 18 139
  • 夜,即墨。 希望旋即遮蓋。 一切變得微不足道。 靜等黎明的到來(lái), 即使曇花一現(xiàn)。 這樣,足夠。 陌上花開(kāi) 2017...
    玫瑰花的夢(mèng)閱讀 119評(píng)論 0 1
  • 建議閱讀時(shí)間:10分鐘 我知道一切都不可能回去了。但是倘若有可能,我是多么希望,能讓你在一個(gè)晴朗有風(fēng)、小憩過(guò)沒(méi)有任...
    真真徐來(lái)閱讀 19,360評(píng)論 133 744
  • 寫(xiě)作是心靈和文字的舞蹈,是思想的一次遠(yuǎn)足,像我這樣有一出沒(méi)一出的簡(jiǎn)書(shū),充其量算作陽(yáng)臺(tái)散步。 于我而言,寫(xiě)作就是流動(dòng)...
    水元閱讀 533評(píng)論 2 3