4

I am trying to make a console application so as to troubleshoot my question here

I have put the source on Git Hub

The console application project file is

<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 is

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();
        }
    }
}

The netcoreapp3.1 project file is

<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>

The framework project file is

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

I get build errors

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   

[Update] I understand that the best way would be to use .netstandard

However I want to explore just getting .netcore and .net472 to work together.

After correcting the console project to be

<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>

I still get build errors in program.cs build errors

Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • Are you trying to load both projects for both platforms, or one for each platform? I will update my answer after this info. – Julian Jun 08 '20 at 06:29
  • How do you expect `MopFW` to be valid in the .NET Core version, when you don't have a reference to `MopFW` in that case? You may want `#if` directives to only build the appropriate version of the code in the console app. – Jon Skeet Jun 08 '20 at 06:48
  • I must be missing something fundamental in my understanding. So a .netcore app cannot call a framework libary? I wanted to load both projects for the core platform. – Kirsten Jun 08 '20 at 07:00
  • I think my mistake was trying to use Entity Framework in the console app. It should stay in the library dlls. – Kirsten Jun 08 '20 at 07:11

1 Answers1

4

You need conditional references.

In your csproj:

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

OR you need to make MopCore also compatible for net472. E.g. implement netstandard2.0.

Julian
  • 33,915
  • 22
  • 119
  • 174