【问题标题】:System.IO.FileLoadException when trying to test Entity Framework Core using MSTest - why?尝试使用 MSTest 测试 Entity Framework Core 时出现 System.IO.FileLoadException - 为什么?
【发布时间】:2020-07-24 20:28:24
【问题描述】:

我无法使用 MSTest 对 Entity Framework Core 项目进行单元测试。

当我这样做时,我在执行测试时总是得到以下异常:

Test method "EF_Tests.CreateDbTests.CreateDb" threw exception: 
    System.IO.FileLoadException: Could not load file or assembly "System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

我做错了什么?



这是我的项目结构和设置:



这是我的项目文件的内容:

Repository.csproj

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
  </ItemGroup>

</Project>

EF-DB.csproj

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <RootNamespace>EF_DB</RootNamespace>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <DebugType>none</DebugType>
    <DebugSymbols>false</DebugSymbols>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.6" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Repository\Repository.csproj" />
  </ItemGroup>

</Project>

EF-Tests.csproj

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{538D0C25-637D-4CDF-BDBC-26AEEB9117FD}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>EF_Tests</RootNamespace>
    <AssemblyName>EF-Tests</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
    <IsCodedUITest>False</IsCodedUITest>
    <TestProjectType>UnitTest</TestProjectType>
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>none</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>
  <PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="CreateDbTests.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\EF-DB\EF-DB.csproj">
      <Project>{c6c34842-8e25-4bcb-a20a-6c415baa008e}</Project>
      <Name>EF-DB</Name>
    </ProjectReference>
    <ProjectReference Include="..\Repository\Repository.csproj">
      <Project>{b4e6eeda-2439-4674-b127-0fbf91b90a08}</Project>
      <Name>Repository</Name>
    </ProjectReference>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="MSTest.TestAdapter">
      <Version>2.1.2</Version>
    </PackageReference>
    <PackageReference Include="MSTest.TestFramework">
      <Version>2.1.2</Version>
    </PackageReference>
  </ItemGroup>
  <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

【问题讨论】:

  • 请显示您的 csproj 文件的内容。
  • @granadaCoder:感谢您回答我的问题。我现在添加了 .csproj 文件的内容。

标签: mstest entity-framework-core-3.1


【解决方案1】:

我就是这么想的。您的测试 csproj 全部“框架化”了。

你想要这样的东西:(来自 .net CORE mstest 项目)(见下图)

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

  <PropertyGroup>
    <TargetFrameworks>netcoreapp3.1</TargetFrameworks>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Chronos.Abstractions" Version="2.0.24" />
    <PackageReference Include="coverlet.msbuild" Version="2.8.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="FluentAssertions" Version="5.10.2" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="Moq" Version="4.13.1" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
    <PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
    <PackageReference Include="coverlet.collector" Version="1.2.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="PasswordGenerator" Version="2.0.5" />
  </ItemGroup>





</Project>

【讨论】:

  • 首先:非常感谢您为我指明了正确的方向! ? 我目前正在将 EF6 示例项目转换为 EFC。 --- 不过,我还有一个问题:我使用 .NET Standard 2.0 作为其他项目的项目类型。那么,为什么我的第一个版本没有按预期工作?应该,不是吗? .NET Standard 2.0 应该可以在这两个世界中工作,不是吗?
  • 这仍然很奇怪...... .NET Framework 项目(即 EF-Test)是 消费 项目;即最后一行。其他支持项目不公开 .NET Standard 以外的任何内容。 ...我想这一定与引用包中的程序集如何被 .NET Framework 与 .NET Core 项目的输出(即\bin\ 目录)捆绑有关。
  • 我不知道。但是当最初的 dotnetFW 说它可以做 vb6ish COM 对象时提醒我。我放弃了那个巫毒。 :)
猜你喜欢
  • 2021-03-04
  • 1970-01-01
  • 2021-10-08
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多