Is it a good idea to target both ef core 3.1 and 5?

Viewed 198

I have a small nuget package that interacts with a small DB with 2 tables. I have used it in 2 projects, 1 is dotnet 5 and the other is an old dotnet framework 4.6 therefore I have targeted netstandard2.0 and it worked fine so far.

I need to update all the packages in the dotnet 5 one to the latest, but ef core 5 needs dotnet standard 2.1. What is the best way to deal with it?

I thought about multi targeting, but first I don't know if it is a good idea or not and then I don't know how to say target dot net standard 2.1 with ef 5 and at the same time dotnet standard 2 with the ef 3.1.

1 Answers

Targeting multiple versions of EF Core (or any other package) can be OK, but bear in mind that it's going to affect the maintainability of the package, so only you can decide if it's worth it or not.

  • Are you going to benefit from any specific feature/performance improvement of EF Core 5?
  • Are different behaviors acceptable for you and your users? If not, revisit the previous point because you may need to tweak your code to achieve that, ignoring some EF Core 5 add-ons.
  • etc.

Regarding the implementation:

You can solve the project references issue by using Condition at ItemGroup level and checking the value of $(TargetFramework).

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.13" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard2.0' ">
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.4" />
</ItemGroup>

In your C# code you'll need to make use of preprocessor directives (#if NETSTANDARD2_0) to differentiate both implementations.

You can read more detailed documentation about both .csproj and C# solutions here.

Related