migration.net-framework-version.net-standard-2.0target-framework

Migrate project from .NET Framework 4.5 to .NET Standard 2.0


I've got a .NET Framework 4.5 project and I want to migrate it to .NET Standard 2.0.

When I try to open this project a windows pops up telling me project is targeting .NET Framework 4.5 and it's no longer supported. It also requires that I update the target to .NET Framework 4.8 or I download .NET Framework4.5 directly from the browser. Otherwise, I can't load the proyect.

But I don't want to do either of those.

Searching on the internet I found that you just have to update .csproj, choose "netstandard2.0" as the TargetFramework and update packages with NuGet.

So my .csproj was something like this:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{756DBCF8-3349-4CAE-B58C-47B7998BE3FF}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Wke.Cms</RootNamespace>
    <AssemblyName>Wke.Cms</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>
...
More below
...

and I simply made this small change opening the file on my file explorer:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{756DBCF8-3349-4CAE-B58C-47B7998BE3FF}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Wke.Cms</RootNamespace>
    <AssemblyName>Wke.Cms</AssemblyName>
    <TargetFramework>netstandard2.0</TargetFramework>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>

Changed "TargetFrameworkVersion" for "TargetFramework" and put "netstandard2.0"

Now I can load the project, but if I try to open project properties I get the following error:

12/04/2024 10:42:18
Recoverable
System.ArgumentException: Expected 1 values for property Build::DefineConstants, but got 0. The property page may not be defined for all configurations.
Nombre del parĂ¡metro: values
   en Microsoft.Requires.Argument(Boolean condition, String parameterName, ValidationInterpolatedStringHandler& message)
   en Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.Property.Update(ImmutableArray`1 values)
   en Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.Property..ctor(PropertyMetadata metadata, ImmutableArray`1 values, PropertyContext context, ImmutableHashSet`1 varyByDimensions)
   en Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.PropertyContextFactoryBase.ToProperty(IUIPropertySnapshot property, Page page, Category category, Int32 order, PropertyContext propertyContext, IPropertyEditorRegistry propertyEditorRegistry)
   en Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.ProjectPropertyDataAccess.Observer.<HandleDataAsync>g__CreateProperties|14_5(<>c__DisplayClass14_0& )
   en Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.Designer.ProjectPropertyDataAccess.Observer.<HandleDataAsync>g__ProcessInitialData|14_1(<>c__DisplayClass14_0& )

Tried to google it and found this, but didn't work for me.

What am I missing here? Do I need to make more changes? Any help is more than welcome.


Solution

  • It is probably the easiest to upgrade your csproj to the new syntax. Your csproj is now still in the old style, which complicates things. Try updating it and re-add your nugets and references again and you should be good to go.

    Your csproj should look something like this:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <RootNamespace>Wke.Cms</RootNamespace>
        <AssemblyName>Wke.Cms</AssemblyName>
        <TargetFramework>netstandard2.0</TargetFramework>
      </PropertyGroup>
    
    </Project>