运行 添加迁移时无法加载程序集 Microsoft.EntityFrameworkCore.Design

Could not load assembly Microsoft.EntityFrameworkCore.Design when running Add-Migration

我已经使用以下 .csproj 创建了一个 Class 库项目:

  <Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.sqlserver.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
  </ItemGroup>

</Project>

当运行命令时:

Add-Migration -Name Test -Context TestContext

我收到以下错误:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.EntityFrameworkCore.Design, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'Microsoft.EntityFrameworkCore.Design, Culture=neutral, PublicKeyToken=null'
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks, IntPtr ptrLoadContextBinder)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks, IntPtr ptrLoadContextBinder)
   at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
   at Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor..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.MigrationsAddCommand.Execute()
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
Could not load file or assembly 'Microsoft.EntityFrameworkCore.Design, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

然而 Microsoft.EntityFrameworkCore.Design 已经安装。

我正在使用 Visual Studio 社区 2017 (VisualStudio/15.0.0+26228.9)

我错过了什么?

在添加 Microsoft.EntityFrameworkCore.Tools.DotNet 并将 dotnet 工具的版本更改为 1.0.0-msbuild2-final 后它起作用了,现在我的 .csproj 是:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.sqlserver.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.0.0-msbuild2-final" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild2-final" />
  </ItemGroup>

</Project>

对我来说,将启动项目设置到 class 库后效果很好,您也可以在不设置启动项目的情况下使用命令

Add-Migration -project [ProjectName] initDB

这里还有我的.csproj 供参考

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />  
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
</ItemGroup>


</Project>

我遵循了 Bart Lannoeye 的指示:

UWP 应用无法在 AnyCPU 配置中 运行 我将我的项目更改为 x86 中的 运行 并且迁移开始工作。

http://www.bartlannoeye.com/blog/ef-core-migrations-throws-exception-calling-createinstanceandunwrap

我的问题是 Visual Studio (15.4.5) 的特定版本无法读取 IDE 设置。 From this workaround from Ben Day 我在命令行上明确设置了安装项目和迁移项目。

这两个命令以这种方式格式化以使其工作。

PM> cd [migrations directory]
PM> Add-Migration -Name [version label] -Context [your context] -s [startup project]