.NET 标准 2.0 项目无法加载 Dapper 1.50.5

.NET standard 2.0 project not able to load Dapper 1.50.5

我刚刚开始使用 .NET 标准。在一个概念验证项目中,我正在尝试使用 Dapper 作为我的 ORM。在 .NET Standard 2.0 class 库项目中,我添加了 Dapper 1.50.5 Nuget 包。但是,程序集不会在运行时加载。 我收到此错误:

System.IO.FileNotFoundException   HResult=0x80070002
Message=Could not load file or assembly 'Dapper, Version=1.50.5.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
The system cannot find the file specified.

我的 .csproj 的完整内容:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Dapper" Version="1.50.5" />
    <PackageReference Include="Npgsql" Version="4.0.4" />
    <PackageReference Include="System.Data.SqlClient" Version="4.6.0" />
    <PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
    <PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.1" />
    <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
    <PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.1" />
  </ItemGroup>
</Project>

所以,你看我安装了依赖和依赖的依赖

我还应该做什么?

.NETStandard 程序集已添加为对我的 WPF 项目的引用。我需要在 WPF 项目的 .csproj 中进行更改。

https://github.com/dotnet/sdk/issues/901中提到的解决方案修复了它。

步骤:

  1. 在记事本中编辑您的核心 .csproj 文件。
  2. 在其中找到的每行中添加以下两行。

    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    
  3. 清理并重建您的解决方案。