Home > Blockchain >  Is it possible to use the same version for multiple dependencies in a csproj file?
Is it possible to use the same version for multiple dependencies in a csproj file?

Time:12-17

I have a C# project (.NET6) that looks like this:

project.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    ...
  </PropertyGroup>
  ...
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
    ...
  </ItemGroup>
  ...
</Project>

Is it somehow possible to maintain these dependency-versions in one place (they all belong together)?
Similar to what e.g. Maven allows by using a property?

The problem is that when I want to update - e.g. to 6.0.1 - I always have to update all dependency-versions at once. Which is in particularly a problem when using automatic tools like Dependabot as they usually create one pull request for each dependency because they are unable to recognize that these dependencies belong together.

I also checked but I didn't find any other solutions so far, neither on StackOverflow nor in the Microsoft docs.

CodePudding user response:

Yes, that works. Just use a variable for the versions.

Like so:


  <PropertyGroup>
    <EntityFrameworkVersion>6.0.0</EntityFrameworkVersion>
  </PropertyGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="$(EntityFrameworkVersion)" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="$(EntityFrameworkVersion)" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="$(EntityFrameworkVersion)" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="$(EntityFrameworkVersion)" />
    ...
  </ItemGroup>

Now you only have to edit a single line to update the version. I normally even move the variable definition to the top-level Directory.build.props file, so that I only need to edit one single line to update the version across all projects in the solution.

There are two possible problems with this solution:

  • I'm not sure (and never really tested) whether dependabot is able to resolve this. It might not be.
  • From experience, when the solution is large (many projects that have this dependency) updating the Directory.build.props file often crashes Visual Studio. When closing it before changing the version, everything is fine, though.

CodePudding user response:

Firstly, you can simplify your references because the other 2 are dependencies and will be automatically included.

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.0" />
</ItemGroup>

If you did want them to share the version, you can define a property for the version:

<PropertyGroup>
    <EfVersion>6.0.0</EfVersion>
<PropertyGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="$(EfVersion)" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="$(EfVersion)" />
</ItemGroup>

However, I wouldn't recommend this - as soon as you use any tooling (like the Nuget Package Manager) to update these references, you will likely run into problems (from memory it will replace the EfVersion with the concrete version, but that will be a tool-specific implementation detail).

  • Related