当从多框架控制台应用程序引用 .netcore dll 时,NU1201 项目与 net472 不兼容

NU1201 Project is not compatible with net472 when a .netcore dll is referenced from a multiframework console app

我正在尝试制作控制台应用程序以便

我已经把源放在 Git Hub

控制台应用程序项目文件是

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp3.1;net472</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\mopcore\MopCore.csproj" />
    <ProjectReference Include="..\MopFW\MopFW.csproj" />
  </ItemGroup>
</Project>

Program.cs 是

using System;
using System.Linq;
using MopCore;
using MopFW;  // error shows here
namespace ConsoleAppCore2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            CountMopsCore();
            CountMopsFramework();
        }

        private static void CountMopsCore()
        {
            Console.WriteLine($"Hi");
            using (var context = new MopContext())
            {
                var num = context.Mops.Count();
                Console.WriteLine($"There are {num} mops \r\n providern {context.Database.ProviderName}");
            }
            Console.ReadKey();
        }

        private static void CountMopsFramework()
        {
            Console.WriteLine($"Hi");
            using (var context = new MopFW.MopContext())
            {
                var num = context.Mops.Count();
                Console.WriteLine($"There are {num} mops \r\n in {context.Database.Connection.ConnectionString}");
            }
            Console.ReadKey();
        }
    }
}

netcoreapp3.1工程文件为

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AssemblyName>MopCore</AssemblyName>
    <RootNamespace>MopCore</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
  </ItemGroup>
</Project>

框架项目文件为

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="EntityFramework" Version="6.4.4" />
  </ItemGroup>
</Project>

我遇到构建错误

Error   NU1201  Project MopCore is not compatible with net472 (.NETFramework,Version=v4.7.2). Project MopCore supports: netcoreapp3.1 (.NETCoreApp,Version=v3.1)    ConsoleAppCore2 C:\Users\kirst\source\repos\MopData\ConsoleAppCore2\ConsoleAppCore2.csproj  1   

[更新] 我知道最好的方法是使用 .netstandard

但是我想探索如何让 .netcore 和 .net472 一起工作。

将控制台项目更正后

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp3.1;net472</TargetFrameworks>
  </PropertyGroup>
    <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
        <ProjectReference Include="..\mopcore\MopCore.csproj" />
    </ItemGroup>
    <ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
        <ProjectReference Include="..\MopFW\MopFW.csproj" />
    </ItemGroup>
</Project>

我仍然在 program.cs 中遇到构建错误

您需要条件引用。

在你的 csproj 中:

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
    <ProjectReference Include="..\mopcore\MopCore.csproj" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
    <ProjectReference Include="..\MopStandard\MopFW.csproj" />
</ItemGroup>

或者您需要使 MopCore 也兼容 net472。例如。实施 netstandard2.0.