使用Scaffold-DBContext设置数据库优先EF项目

时间:2017-02-22 14:15:39

标签: asp.net-core entity-framework-core

我有一个数据库,我正在尝试设置数据库的第一个EF项目。我的项目是一个使用.Net Core的Web API项目。像我发出的命令是:

Scaffold-DbContext "Server=Server\Instance;Database=MyDatabase;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

但是,我收到以下错误:

Could not load file or assembly 'Microsoft.EntityFrameworkCore.Design' or one of its dependencies. The system cannot find the file specified.

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.EntityFrameworkCore.Design' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.EntityFrameworkCore.Design'
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at Microsoft.EntityFrameworkCore.Tools.AppDomainOperationExecutor..ctor(String assembly, String startupAssembly, String projectDir, String contentRootPath, String dataDirectory, String rootNamespace, String environment)
   at Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor()
   at Microsoft.EntityFrameworkCore.Tools.Commands.DbContextScaffoldCommand.Execute()
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)

此项目的依赖项如下:

  "dependencies": {
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
    "Microsoft.NETCore.App": "1.1.0",
    "NETStandard.Library": "1.6.1",
    "Microsoft.ApplicationInsights.AspNetCore": "2.0.0",
    "Microsoft.AspNetCore.Mvc": "1.1.1",
    "Microsoft.AspNetCore.Routing": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.Extensions.Logging.Debug": "1.1.0",
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-msbuild3-final",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.Relational.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "System.Collections.NonGeneric": "4.3.0",
    "Microsoft.AspNetCore.Hosting.Abstractions": "1.1.0"
  },

如您所见,它被引用。

请有人指点我这个方向的正确方向吗?

编辑:

在它之前是当前(完整)project.json:

{
  "dependencies": {
    "Microsoft.ApplicationInsights.AspNetCore": "2.0.0",
    "Microsoft.AspNetCore.Hosting.Abstractions": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.1.1",
    "Microsoft.AspNetCore.Routing": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.Relational.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",    
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4-final",
    "Microsoft.Extensions.Configuration.Json": "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.NETCore.App": "1.1.0",
    "NETStandard.Library": "1.6.1",
    "System.Collections.NonGeneric": "4.3.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

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

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "runtimes": {
    "win10-x64": {}
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

使用此配置,我收到编译错误:

Can not find runtime target for framework '.NETCoreApp,Version=v1.0' compatible with one of the target runtimes: 'win10-x86, win81-x86, win8-x86, win7-x86'. Possible causes:   JourneyService  C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets  262 Build   

1 个答案:

答案 0 :(得分:2)

请执行以下步骤。 将.Net Core SDK更新为最新的最新版本。 (对于新的1.0.0-preview2-1-003177)。下载链接 在gobal.json文件中更新.Net Core SDK版本 在project.json的frameworks部分更新netcoreapp版本 "框架":{  " netcoreapp1.1":{    "进口":[      " dotnet5.6&#34 ;,      "便携式net45 + win8的"     ]  } }, 将这些包添加到`project.json " Microsoft.EntityFrameworkCore.SqlServer.Design":{   "版本":" 1.1.0",   "输入":" build" }, " Microsoft.EntityFrameworkCore.Tools":{   "版本":" 1.1.0-preview4-final",   "输入":" build" } 更新project.json中的工具部分并添加Microsoft.EntityFrameworkCore.Tools最新版本 "工具":{    " Microsoft.EntityFrameworkCore.Tools":" 1.1.0-preview4-final" }, 命令: `Scaffold-DbContext" Server = Server \\ Instance; Database = MyDatabase; Trusted_Connection = True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models` 完成project.json文件。 {   "依赖":{     " Microsoft.ApplicationInsights.AspNetCore":" 2.0.0",     " Microsoft.AspNetCore.Hosting.Abstractions":" 1.1.0",     " Microsoft.AspNetCore.Mvc":" 1.1.1",     " Microsoft.AspNetCore.Routing":" 1.1.0",     " Microsoft.AspNetCore.Server.IISIntegration":" 1.1.0",     " Microsoft.AspNetCore.Server.Kestrel":" 1.1.0",     " Microsoft.EntityFrameworkCore":" 1.1.0",     " Microsoft.EntityFrameworkCore.Design":" 1.1.0",     " Microsoft.EntityFrameworkCore.Relational.Design":" 1.1.0",     " Microsoft.EntityFrameworkCore.SqlServer":" 1.1.0",     " Microsoft.EntityFrameworkCore.Tools.DotNet":" 1.1.0-preview4-final",     " Microsoft.Extensions.Configuration.Json":" 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.NETCore.App":{       "版本":" 1.1.0",       "输入":"平台"     },     " NETStandard.Library":" 1.6.1",     " System.Collections.NonGeneric":" 4.3.0",     " Microsoft.EntityFrameworkCore.SqlServer.Design":{       "版本":" 1.1.0",       "输入":" build"     },     " Microsoft.EntityFrameworkCore.Tools":{       "版本":" 1.1.0-preview4-final",       "输入":" build"     }   },   "工具":{     " Microsoft.AspNetCore.Server.IISIntegration.Tools":" 1.0.0-preview2-final",     " Microsoft.EntityFrameworkCore.Tools":" 1.1.0-preview4-final"   },   "框架":{     " netcoreapp1.1":{       "进口":[         " dotnet5.6&#34 ;,         "便携式net45 + win8的"       ]     }   },   " buildOptions":{     " emitEntryPoint":是的,     " preserveCompilationContext":true   },   " runtimeOptions":{     " configProperties":{       " System.GC.Server":true     }   },   "运行时":{     " win10-x64":{}   },   " publishOptions":{     "包括":[       " wwwroot的&#34 ;,       " ** / * CSHTML&#34 ;,       " appsettings.json&#34 ;,       "的web.config"     ]   },   "脚本":{     " postpublish":[" dotnet publish-iis --publish-folder%publish:OutputPath% - framework%publish:FullTargetFramework%" ]   } }