4

I've put together a .Net 5 for Windows application, using among other things EntityFramework 5.0.13.

Now we're trying to run it on a specific server, that doesn't have the .Net 5 framework yet. The nice people who admin the server tell me that they've tried to install new things, but can't seem to make the application run. Restarting the server might be necessary, which cannot be done for the time being.

So I need to use an older target framework for my application. Fine, so I decided to target multiple frameworks, adding .Net Framework 4.8 as well. In my .csproj file, that amounts to replacing

    <TargetFramework>net5.0-windows</TargetFramework>

with

    <TargetFrameworks>net5.0-windows;net48</TargetFrameworks>

and that should do the trick.

Unfortunately, EntityFramework 5.0.13 is not compatible with .Net Framework 4.8.

So I used an older version, 3.1.9 (that works fine on another of our projects).

It took a bit of convincing, but now my project uses 3.1.9. (And I trust that it will build fine once I downgrade a few Class variable = new(); and if (variable is not null) statements.)

Now, what I'm wondering about is, can I get my .Net 5 target framework to use EntityFramework 5.0.13 again instead of 3.1.9?

Right now, my .csproj file reads

    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.9" />

Before I started to use multiple target frameworks, that was

    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.13" />

Is there a way to add an attribute to PackageReference (or replace it with another node) so it only targets a specific framework?

Jean-David Lanz
  • 865
  • 9
  • 18
  • 2
    [Yes](https://learn.microsoft.com/nuget/consume-packages/package-references-in-project-files#adding-a-packagereference-condition). (If you're doing this for multiple references, it's cleaner to make multiple `ItemGroup`s and put the condition there, though the UI may not always understand what you're doing then and put references in the wrong group if you install packages from there.) – Jeroen Mostert Mar 07 '22 at 14:06
  • Thanks for the tip! I've been careful to use it in multiple `ItemGroup`s. As I said below, it works like a charm! – Jean-David Lanz Mar 07 '22 at 14:42

1 Answers1

6

A simple way to do it is to add the <PackageReference ...> nodes conditionally:

<ItemGroup>
    <!-- ... -->
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.9" Condition="'$(TargetFramework)' == 'net48'" />
    <!-- ... -->
</ItemGroup>

This is a modified example from https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#adding-a-packagereference-condition, which gives more details if required.

Andrew McClement
  • 1,171
  • 5
  • 14
  • That works like a charm! (I could even use similar tactics to reference an internal project in the same ways.) – Jean-David Lanz Mar 07 '22 at 14:32
  • 6
    Note too that if you want to condition multiple items by framework, you can move the `Condition="..."` to the `ItemGroup`. – Drew Noakes Mar 08 '22 at 08:52
  • I use this in a Razor Class Library to support .net7.0 and net6.0 but the Visual Studio Nuget manager apparently can't handle it and incorrectly tells me that my Razor lib has Nuget updates available when it does not. I am using Visual Studio 17.5.0 Preview 2.0 Community Edition. – LoneSpawn Jan 12 '23 at 14:25
  • According to the link below Microsoft has no intention of supporting multiple target frameworks in the Visual Studio UI. https://developercommunity.visualstudio.com/t/vs-lack-support-for-c-project-with-multiple-target/896987 "At this point we don’t have a plan to provide support in the UI for Multi-Targeting. Our current solution is to use the project file." Juan José Mejia [MSFT] – LoneSpawn Jan 12 '23 at 14:39