26

I've inherited a VS-2015 C# application and would like to migrate it to VS 2017 or 2019. It has a packages.config file with 4 packages:

<package id="AjaxControlToolkit" version="15.1.4.0" targetFramework="net4" />
<package id="EntityFramework" version="6.0.0" targetFramework="net4" />
<package id="Microsoft.AspNet.Providers" version="2.0.0" targetFramework="net4" />
<package id="Microsoft.AspNet.Providers.Core" version="2.0.0" targetFramework="net4" />

The first few lines of the project's sln file are:

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}")

I'd like to migrate the packages.config file to a csproj file.

In Visual Studio 2017, I tried migrating it by right-clicking on packages.config and clicking 'Migrate packages.config to PackageReference', but it gives me an error:

Operation Failed - Project is not Eligible for Migration.

error message

I also tried this tool: https://github.com/hvanbakel/CsprojToVs2017 and this also fails.

Is there really no way to migrate this to .csproj?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
VBStarr
  • 586
  • 6
  • 17

2 Answers2

53

There is a workaround to this issue that I have used for older ASP.NET projects (and may potentially work with other project types which exhibit this issue too).

Essentially, the migration tool can still work, but first you must fool Visual Studio (temporarily) into thinking your project is a class library rather than a web project while you do the migration.

Detailed steps:

  1. Close your project/solution if it's open in Visual Studio, and take a backup of it.

  2. Open the .csproj file in a text editor

  3. Cut the ProjectGuid and ProjectTypeGuids entries, and paste them somewhere else temporarily.

  4. Insert <ProjectGuid>{7C796B6B-86B5-4C57-ADAA-12CF1FECDA71}</ProjectGuid> where your ProjectGuid entry was previously, and save the file

  5. Open the project in Visual Studio (2017 or later)

  6. Right-click the packages.config file and choose the option to migrate to package reference.

  7. Once the migration is successful, close the project/solution again.

  8. Go back into the .csproj file and replace the ProjectGuid entry with the ProjectGuid and ProjectTypeGuids entries which you saved in step 3.

  9. Re-open the project in Visual Studio. You should now be able to use it as normal.

This has worked for me on two projects so far. I can't promise there wouldn't be any side-effects in edge cases or more complex projects, but it's certainly something you can try.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 2
    Thanks for this! It worked for me as well, migrating a 4.6.1 aps.net web app – cameronjchurch Feb 10 '21 at 15:35
  • I breathed wrong around my project and it stopped building because "Must use ProjectReference". This fixed it! It did reset the port number in Properties. It might reset everything to defaults, but that was the only non-default setting so that was all I had to change back. – jspinella Mar 15 '21 at 17:44
  • @jspinella glad it helped you. I think there are one or two nuget packages which require packagereference after a certain version. So it could have been an upgrade to such a package which caused your issue. I don't know why that requirement exists but I noticed it on something a while back and got the same error as you...I forget the specific package it was unfortunately. – ADyson Mar 15 '21 at 17:55
  • 2
    I can't edit my previous comment but this fix also changed the Web.config file to App.config. Something for others to be aware of. I've been fighting with Microsoft.Extensions.XXXX packages. – jspinella Mar 15 '21 at 18:17
  • 1
    @Oleg Grishko many thanks! this worked for me with a small difference: 1. I've unloaded the project from the solution, commented the {ProjectGuid and ProjectTypeGuids}, added your ProjectGuid. 2. reloaded the project and migrated its package reference. 3 unloaded and switched back to original {ProjectGuid and ProjectTypeGuids} with selection for Top-Level additional references – Vadim Tofan Nov 05 '21 at 19:08
  • @VadimTofan Oleg just edited the answer slightly to correct a typo. It was me who wrote it :-) – ADyson Nov 05 '21 at 20:01
  • 2
    You can also just do this by unloading the project, double-clicking the .csproj to edit it in Visual Studio, and reload it rather than using a text editor. I used notepad anyway to paste the old GUIDs but did the editing within VS. You may have to delete the generated app.config and re-set it as the Startup Project. – Literate Corvette Dec 08 '21 at 20:00
  • The github link does not work anymore. – shanji97 Oct 27 '22 at 06:56
  • 1
    @SH4NNY1 Thanks for the heads-up, that's unfortunate. But most, if not all of the information you need in order to carry this out is preserved in this answer anyway. – ADyson Oct 27 '22 at 09:30
  • 5
    Thanks a lot for this! It also seems achievable by just commenting out the `ProjectTypeGuids` element before migration and leaving the `ProjectGuid` alone. – jmkjaer Jan 05 '23 at 12:56
  • 1
    Worked for me in VS2022 – Andrew Steitz May 16 '23 at 12:53
6

According to the documentation:

Migration is not currently available for C++ and ASP.NET projects.

The migration (through right-clicking on the packages file) works fine for console applications for example.

Mahdi
  • 3,199
  • 2
  • 25
  • 35
  • 2
    This is a C# based project, not C++. Is there any way to do a migration to PackageReference? – VBStarr Aug 21 '19 at 16:05
  • 1
    @BeachBum and ASP.NET projects regardless of the language, so unfortunately not at the moment. – Mahdi Aug 21 '19 at 16:18
  • 1
    More details on ASP.NET-specific limitations: https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#project-type-support This references https://github.com/NuGet/Home/issues/5877 with the gory specifics. – J Bryan Price Jan 16 '20 at 19:34
  • (reworded from 2021 comment) This is the correct answer. I attempted @ADyson's work-around and it caused quite a few hard to track issues in our distributed monolith. As linked by JBryanPrice, Microsoft explicitly says one can't do this conversion (yet). After a day troubleshooting bugs caused, we gave up and rolled back. – AppFzx Jan 10 '23 at 13:33