简体   繁体   中英

Multiple target framework project: different versions of the same NuGet package on different frameworks?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM