无法在 Mac 上使用 dotnet ef 核心搭建脚手架

Posted

技术标签:

【中文标题】无法在 Mac 上使用 dotnet ef 核心搭建脚手架【英文标题】:Not able to scaffold with dotnet ef core on mac 【发布时间】:2017-03-31 09:04:17 【问题描述】:

我正在尝试使用 .net 核心从我的 MAC 上的现有数据库中逆向工程模型类。数据库在带有 Windows 的 VM 中运行,我可以从 osx 连接到它。

在我的 project.json 中,我有以下内容:

"dependencies": 
    "Microsoft.NETCore.App": 
      "version": "1.0.1",
      "type": "platform"
    ,
    "Microsoft.AspNetCore.AngularServices": "1.0.0-*",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Razor.Tools": 
      "version": "1.0.0-preview2-final",
      "type": "build"
    ,
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",

    "Microsoft.EntityFrameworkCore.Design": 
      "type": "build",
      "version": "1.0.0-preview2-final"
    ,
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final"
  ,

  "tools": 
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.DotNet.Watcher.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  ,

  "frameworks": 
    "netcoreapp1.0": 
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    
  ,

  "buildOptions": 
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
    "compile": 
      "exclude": ["node_modules"]
    
  

但是当我跑步时

dotnet ef dbcontext scaffold "myDbConnectString" Microsoft.EntityFrameworkCore.SqlServer

我收到以下错误,我无法通过这个

 An error occurred while calling method 'ConfigureDesignTimeServices' on startup class

'Microsoft.EntityFrameworkCore.Scaffolding.SqlServerDesignTimeServices'。 考虑使用 IDbContextFactory 来覆盖 设计时的 DbContext。错误:无法加载类型 'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions' 从程序集'Microsoft.Extensions.DependencyInjection.Abstractions, 版本=1.0.0.0,文化=中性,PublicKeyToken=adb9793829ddae60'。 System.AggregateException:发生一个或多个错误。 (无法解析服务类型 'Microsoft.EntityFrameworkCore.Scaffolding.IScaffoldingModelFactory' 在尝试激活时 'Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineeringGenerator'。) ---> System.InvalidOperationException:无法解析服务类型 'Microsoft.EntityFrameworkCore.Scaffolding.IScaffoldingModelFactory' 在尝试激活时 'Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineeringGenerator'。 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider 提供者,ISet1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider, ISet1 callSiteChain) 在 Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService 服务,ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(Type serviceType, ISet1 callSiteChain) 在 Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(类型 服务类型,服务提供者服务提供者) 在 System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary2 dictionary, TKey key, Func3 valueFactory,TArg arg) 在 Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型 服务类型) 在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider 提供者,类型 serviceType) 在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider 提供者) 在 Microsoft.EntityFrameworkCore.Design.DatabaseOperations.ReverseEngineerAsync(字符串 提供者,字符串 connectionString,字符串 outputDir,字符串 dbContextClassName,IEnumerable1 schemas, IEnumerable1 个表, Boolean useDataAnnotations, Boolean overwriteFiles, CancellationToken 取消令牌) 在 Microsoft.EntityFrameworkCore.Design.Internal.OperationExecutor.ReverseEngineerAsync(字符串 提供者,字符串 connectionString,字符串 outputDir,字符串 dbContextClassName, IEnumerable1 schemaFilters, IEnumerable1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, CancellationToken 取消令牌) 在 Microsoft.EntityFrameworkCore.Tools.Cli.DbContextScaffoldCommand.d__1.MoveNext() --- 内部异常堆栈跟踪结束 --- 在 System.Threading.Tasks.Task.ThrowIfExceptional(布尔 includeTaskCanceledExceptions) 在 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) System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Scaffolding.IScaffoldingModelFactory' while attempting to activate 'Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineeringGenerator'. at Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet1 callSiteChain,ParameterInfo[] 参数,布尔值 throwIfCallSiteNotFound) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider 提供者,ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(IService service, ISet1 callSiteChain) 在 Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(类型 服务类型,ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider) at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary2 字典,TKey 键,Func3 valueFactory, TArg arg) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.Design.DatabaseOperations.ReverseEngineerAsync(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable1 模式,IEnumerable1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Design.Internal.OperationExecutor.ReverseEngineerAsync(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable1 schemaFilters,IEnumerable`1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, CancellationToken 取消令牌) 在 Microsoft.EntityFrameworkCore.Tools.Cli.DbContextScaffoldCommand.d__1.MoveNext()

我尝试来回更改依赖项的版本,但没有结果。有人指点一下吗?

【问题讨论】:

【参考方案1】:

您是否尝试过使用 RTM 版本的实体框架?


  "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.1",
  "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1"

【讨论】:

不是那些版本,但我只是让它工作。将发布我的新 project.json 以帮助其他人【参考方案2】:

刚刚开始工作。 我更新到 .net core 1.1.0 并修改了我的 project.json 以使用更新的版本。希望这可以帮助其他人

project.json:

"dependencies": 
  "Microsoft.NETCore.App": 
    "version": "1.1.0",
    "type": "platform"
  ,
  "Microsoft.AspNetCore.AngularServices": "1.0.0-*",
  "Microsoft.AspNetCore.Diagnostics": "1.1.0",
  "Microsoft.AspNetCore.Mvc": "1.1.0",
  "Microsoft.AspNetCore.Razor.Tools": 
    "version": "1.1.0-preview4-final",
    "type": "build"
  ,
  "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
  "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
  "Microsoft.AspNetCore.StaticFiles": "1.1.0",
  "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
  "Microsoft.Extensions.Configuration.Json": "1.1.0",
  "Microsoft.Extensions.Configuration.CommandLine": "1.1.0",
  "Microsoft.Extensions.Logging": "1.1.0",
  "Microsoft.Extensions.Logging.Console": "1.1.0",
  "Microsoft.Extensions.Logging.Debug": "1.1.0",
  "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",

  "Microsoft.EntityFrameworkCore.Design": 
    "type": "build",
    "version": "1.1.0"
  ,
  "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
  "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0"
,

"tools": 
  "Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final",
  "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
  "Microsoft.DotNet.Watcher.Tools": "1.1.0-preview4-final",
  "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4-final"

【讨论】:

以上是关于无法在 Mac 上使用 dotnet ef 核心搭建脚手架的主要内容,如果未能解决你的问题,请参考以下文章

zsh:找不到命令:dotnet-ef

dotnet ef 迁移 name dotnet ef 数据库更新

无法在 mac 上安装 dotnet sdk

无法启动 dotnet 核心进程。添加工具失败

由于 dotnet ef dbcontext --json 失败,无法在 Visual Studio 2019 中列出实体框架迁移

如何调试 dotnet ef 数据库删除?