Search code examples
c#csproj

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


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.


Solution

  • 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.