Nuget and .NetCore versions – tips and tricks

The problem

I have had some issues recently upgrading to .Net Core 6.0.

In doing so I decided that I should stick to only Microsoft libraries that started with 6.x, because I was using .Net Core 6 and so it would be incompatible with the .Net Core 7.0 Libraries.

This thinking is actually wrong and I am here to explain why.

.Net Core Versions

I am writing code that is to be deployed to AWS Lambda, which at time of writing only supports .Net Core 6.0.

When upgrading our code to .Net 6.0 I made the decision to limit my Microsoft references to 6.0 versions only. Take EntityFramework or Microsoft.Extensions.Logging.Configuration.

When viewing the nuget package, scroll down and look at the dependencies, if the library is compatible with .Net 6.0 it will be listed in the dependencies, and therefore compatible with .Net 6.0 even through the library is actually a .Net 7.0 Library.

Note: Not All libraries are backwards compatible see the following does NOT list .Net 6 and so is incompatible so you have to stick with the 6.x versions.

Nuget Package Dependencies

You will often see that in code people may have the following nuget package references

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.11" />

However, if you are aware of the package dependencies you generally do not need to include the first reference “Microsoft.EntityFrameworkCore” as this is a dependency of “Microsoft.EntityFrameworkCore.SqlServer”.

So you can have less references and run into less maintenance issues and package dependency issues if you limit your NuGet package references.

.Net Core Nuget package cross library dependencies

In my projects I often create a project to house;

  • My Main application
  • Business Layer
  • Data Access Layer
  • Common – space for enums, constants and appsettings
  • Models
  • Interfaces

If I

  1. add a reference to Microsoft.EntityFrameworkCore.SqlServer to my DAL
  2. Reference my DAL from my Business Layer
  3. Reference my Business Layer from my Application

The I do not need to add a NuGet package reference to EntityFrameworkCore.SqlServer to any of the other layers that either Directly reference the DAL or reference any project that via the chain of references, references the DAL.

So assume that as part of the Startup.cs I need to add Dependency Injection registrations that require EntityFramework e.g. options.UseSqlServer(connectionString), due to the dependency chain I will be able to access this extension and any other classes or methods of EntityFramework.

The dependency chain from App >> Business Layer >> DAL means that App has access to all of these libraries.

Again you only need to add this reference once or twice depending NOT in every project.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.