【问题标题】:Could not load assembly Microsoft.EntityFrameworkCore.Design when running Add-Migration运行 Add-Migration 时无法加载程序集 Microsoft.EntityFrameworkCore.Design
【发布时间】:2017-08-20 13:14:26
【问题描述】:

我已经使用以下 .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.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 Community 2017 (VisualStudio/15.0.0+26228.9)

我错过了什么?

【问题讨论】:

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


    【解决方案1】:

    在添加 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>
    

    【讨论】:

    • 这行得通,但它是关于为什么这是一个要求。
    • EFCore 不应该需要 .NET Core,但“Microsoft.EntityFrameworkCore.Tools.DotNet”需要 .NET Core。叹息。
    【解决方案2】:

    对我来说,将启动项目设置为类库后它工作正常,您也可以使用命令而不设置启动项目来完成它

    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>
    

    【讨论】:

    • 哇。谁能想到将启动项目设置为类库会解决这个问题!解决了我的问题,即使我在打包管理器控制台中选择了类库!
    • 在设置启动项目后为我工作。
    【解决方案3】:

    我听从了 Bart Lannoeye 的指示:

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

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

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2021-09-21
        • 2011-09-09
        • 1970-01-01
        • 2017-02-17
        • 1970-01-01
        • 2017-04-20
        • 2012-08-21
        • 1970-01-01
        相关资源
        最近更新 更多