14

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?

2
  • 2
    Yes. (If you're doing this for multiple references, it's cleaner to make multiple ItemGroups 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.) Commented Mar 7, 2022 at 14:06
  • Thanks for the tip! I've been careful to use it in multiple ItemGroups. As I said below, it works like a charm! Commented Mar 7, 2022 at 14:42

1 Answer 1

17

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.

Sign up to request clarification or add additional context in comments.

4 Comments

That works like a charm! (I could even use similar tactics to reference an internal project in the same ways.)
Note too that if you want to condition multiple items by framework, you can move the Condition="..." to the ItemGroup.
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.
According to the link below Microsoft has no intention of supporting multiple target frameworks in the Visual Studio UI. developercommunity.visualstudio.com/t/… "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]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.