55

I'm writing a class library for a simple parser in C#. When I first created it, I used .NET standard 2.0, but now I need to migrate it to .NET 4.6 both to conform to the other projects in my solution and in order to use NUnit.

I tried to follow the instructions in the Microsoft documentation, but when I try to select another framework in the properties, I can only find other .NET standard versions.

How can I migrate it? Will I need to manually edit the .csproj file?

Edward Minnix
  • 2,889
  • 1
  • 13
  • 26
  • Which version of Visual Studio are you using? – j03p Jul 05 '18 at 14:32
  • Visual Studio Professional 2017 – Edward Minnix Jul 05 '18 at 14:34
  • 2
    It is interesting that you said "now I need to migrate it to .NET 4.6 both to conform to the other projects in my solution and in order to use NUnit". Why do you need to migrate? Typically NUnit can work well with .NET Standard (though the assembly hosting NUnit test cases might need to target net46). – Lex Li Jul 05 '18 at 14:40
  • [The NUnit docs](https://github.com/nunit/docs/wiki/.NET-Core-and-.NET-Standard) say that the assembly that the tests are in either have to be .NET framework or .NET core. 4.6 is because there's 6 projects in the solution, and mine is only one of them (all the others are 4.6) – Edward Minnix Jul 05 '18 at 14:44
  • 4
    It is the point of .netstandard to not have to do this. Why anybody would target a framework version that was quite buggy and quickly had to be updated to 4.6.1 is a fair mystery. Fix that instead, take it to 4.7.1 while you're at it. – Hans Passant Jul 05 '18 at 14:45

4 Answers4

58

Open up the project file (.csproj) and change the TargetFramework to net462

<PropertyGroup>
  <TargetFramework>net462</TargetFramework>
</PropertyGroup>
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Jammer
  • 9,969
  • 11
  • 68
  • 115
32

My personal experience in Visual Studio 2017 is that recreating project and adding existent sources is the simplest, safest and most effective way - because .Net Framework based csproj file has extra xml elements (comparing with Standard based), it seems changing "TargetFramework" is not enough. Below is portion of diffs appeared by default:


enter image description here

Vlad
  • 1,977
  • 19
  • 44
  • 4
    This guy is right. There are much more of changes than in accepted answer. – Andrew_STOP_RU_WAR_IN_UA Jan 04 '19 at 01:56
  • 1
    This is right - do not do as the accepted answer. You may scramble your project and waste a lot of time. IMHE. – Frank Monroe Jun 09 '20 at 20:37
  • 1
    Agreed, don't just change the target framework in xml - there's so much more that needs to go into it to "migrate" anything! Remove the existing project from the solution, rename the folder, create a new project in the same place with the same name, copy your files back, add them to the project, add your references. Two minutes, super safe, super easy. – Jeff Whitty Sep 02 '20 at 14:28
4

If you are publishing your class library as a Nuget package then there is a better way to set this up. Check out this article:

https://weblog.west-wind.com/posts/2017/Jun/22/MultiTargeting-and-Porting-a-NET-Library-to-NET-Core-20

Basically you can setup your class library for multi targeting, allowing it to be imported into .net core projects as well as different versions of .net frameworks.

Marko
  • 12,543
  • 10
  • 48
  • 58
  • Nice. This should be a better known feature. No idea why MS doesn't make it more explicit by allowing multi-select directly on the UI. – Hugo Nava Kopp Dec 21 '20 at 23:08
3

There are a few steps that I did and worked for me:

  1. git push your code, so you have a back up :)
  2. Unload the project in VS (right click on the project and unload)
  3. Edit the project in VS (right click and edit)
  4. Replace the TargetFramework OR/AND TargetFrameworkVersion with <TargetFramework>netcoreapp2.0</TargetFramework>

  5. Change the project line, that's usually the first line (after xml root) to: <Project Sdk="Microsoft.NET.Sdk">

  6. Remove the import that's usually the second line (after the xml root)

  7. Keep your PropertyGroups that describe the build options, if you want (I want mine as are custom)
  8. Remove the references and the file references, they are not needed.
  9. Close the file and reload (right click reload).
  10. Delete the assemblyinfo file (from properties folder) as it is not needed, the assembly version comes now from the proj
  11. Right click on the project and go to properties to see that you don't have any error in proj file. Ensure that you don't have typos or tags that are not close.
  12. Build. If you have dependencies that you are missing then right click and on the project and add them. - I suppose that you don't want to edit them in the proj. Or you can do a dotnet restore or dotnetbulid to add them, as you would like.

Hope this works for you. They seem a lot of steps but they are not that complicated, and this is one time effort.

Community
  • 1
  • 1
Dan
  • 31
  • 2
  • I tried this and the project won't even load up anymore. I get error "The element <#text> beneath element is unrecognized." can't build either. – SendETHToThisAddress May 04 '20 at 19:16
  • It looks like you're converting the other way, from .NETFramework to ,NETStandard - either way, there are a lot of things that can go wrong doing it this way... – DaveN59 Apr 29 '21 at 22:04