.netframework迁移到.netcore方法

  • .netframework迁移到.netcore方法已关闭评论
  • 108 次浏览
  • A+
所属分类:.NET技术
摘要

一 .netframework程序迁移到.netcore5.0
对于.netframwork程序想要升级为.netcore5.0的方法,微软官方也给出了方法见 https://docs.microsoft.com/en-us/dotnet/desktop/winforms/migration/?view=netdesktop-5.0,我这里总结记录一下.

.netframework迁移到.netcore方法

一 .netframework程序迁移到.netcore5.0
对于.netframwork程序想要升级为.netcore5.0的方法,微软官方也给出了方法见 https://docs.microsoft.com/en-us/dotnet/desktop/winforms/migration/?view=netdesktop-5.0,我这里总结记录一下.

1.首先要检查自己应用程序适不适合迁移
.netframework框架和.netcore框架有很大的不同,首先得确保自己当前基于.netframework程序迁移到.netcore上要改变哪些东西。下载迁移分析工具NET Portability Analyzer ,这个可以到visual Studio的管理扩展中进行下载,这个分析工具的作用就是告诉我们,从当前版本升级到.netcore5.0版本,你的程序需要改变的库或者是Nuget包,因为升级后.netcore程序可能不支持以前的的Nuget包或者库.

.netframework迁移到.netcore方法

安装完成后,先要进行迁移框架的选择,比如我先迁移到.netcore5.0上面去

.netframework迁移到.netcore方法

配置目标框架

.netframework迁移到.netcore方法

开始对工程开始进行升级前分析

.netframework迁移到.netcore方法

分析完成后会有生成的报告结果

.netframework迁移到.netcore方法

分析和解释分析报告
Portability Summary

.netframework迁移到.netcore方法

Details(分析细节部分)

.netframework迁移到.netcore方法

细节分析罗列了在目标框架下会确实的API函数

Target type: 目标平台下缺失的API
Target member:目标平台下缺失的方法
Assembly name:.NET Framework下存在已经缺失的API
Missing Assemblies(缺失的程序集)
比如我这里的EntityFramework.dll,这个程序集只支持.netframework4.8的,在.netcore5.0下面是不受支持的,所以报告中会显示出这一项,但是在net5.0下可以用Microsoft.EntityFrameworkCore
下替换,因此还是可以进行迁移。
.netframework迁移到.netcore方法

2.修改项目的工程文件(.csproj)

net5使用sdk风格的项目文件,不会加载net Framework使用的Visual Studio项目文件,因此需要修改工程文件.

2.1首先,在解决方案中要迁移的工程项目进行卸载,然后对项目文件进行编辑

.netframework迁移到.netcore方法

.netframework迁移到.netcore方法

把项目文件中的内容都剪切到别的地方(之后还有用)

2.复制以下文件到项目文件中
<Project Sdk="Microsoft.NET.Sdk">   <PropertyGroup>     <OutputType>WinExe</OutputType>     <TargetFramework>net5.0-windows</TargetFramework>     <UseWindowsForms>true</UseWindowsForms>     <GenerateAssemblyInfo>false</GenerateAssemblyInfo>   </PropertyGroup> </Project>

上述的XML文件给出了工程文件project的基本结构,但是他并没有包含旧工程文件中的工程名相关的信息,因此需要拷贝旧工程文件下的RootNamespaceAssemblyName所在标签中的行的内容(比如我这里面的SqlDemo工程)

<Project Sdk="Microsoft.NET.Sdk">   <PropertyGroup>     <OutputType>WinExe</OutputType>     <TargetFramework>net5.0-windows</TargetFramework>     <UseWindowsForms>true</UseWindowsForms>     <GenerateAssemblyInfo>false</GenerateAssemblyInfo>     <!--工程名相关的信息-->     <RootNamespace>SqlDemo</RootNamespace>     <AssemblyName>SqlDemo</AssemblyName>   </PropertyGroup> </Project>

3.复制旧工程中 < ItemGroup > 下包含 < ProjectReference > or < PackageReference > 到 < PropertyGroup > 到新工程的下的 < ItemGroup >(位于< PropertyGroup >后面),并且可以移除旧版本中的< project >和 < name >标签

<Project Sdk="Microsoft.NET.Sdk">   <PropertyGroup>     <OutputType>WinExe</OutputType>     <TargetFramework>net5.0-windows</TargetFramework>     <UseWindowsForms>true</UseWindowsForms>     <GenerateAssemblyInfo>false</GenerateAssemblyInfo>     <!--工程名相关的信息-->     <RootNamespace>SqlDemo</RootNamespace>     <AssemblyName>SqlDemo</AssemblyName>   </PropertyGroup>        <ItemGroup>        <!--包含另外一个工程的路径,移除旧版本中的<project>和<name>标签-->       <ProjectReference Include="..SqlDataSetLibSqlDataSetLib.csproj"/>     </ItemGroup>     <ItemGroup>     <!--引用的Nuget包名或者某个外部程序集以及填写版本号,不支持的程序集不要引进来-->       <PackageReference Include="Microsoft.EntityFrameworkCore">          <Version>3.1.4</Version>       </PackageReference>    </ItemGroup>     </Project>
4.复制旧版本中的资源和设置项标签

旧版本中如果有设置项的资源,如下面所示的这样,直接把include改为Update加入到新工程中

<ItemGroup> <!--设置文件-->   <None Update="PropertiesSettings.settings">     <Generator>SettingsSingleFileGenerator</Generator>     <LastGenOutput>Settings.Designer.cs</LastGenOutput>   </None>   <Compile Update="PropertiesSettings.Designer.cs">     <AutoGen>True</AutoGen>     <DependentUpon>Settings.settings</DependentUpon>     <DesignTimeSharedInput>True</DesignTimeSharedInput>   </Compile> </ItemGroup>

复制资源文件,同理,把资源文件的include替换成Update即可

<ItemGroup>   <EmbeddedResource Update="PropertiesResources.resx">     <Generator>ResXFileCodeGenerator</Generator>     <LastGenOutput>Resources.Designer.cs</LastGenOutput>   </EmbeddedResource>   <Compile Update="PropertiesResources.Designer.cs">     <AutoGen>True</AutoGen>     <DependentUpon>Resources.resx</DependentUpon>     <DesignTime>True</DesignTime>   </Compile> </ItemGroup>

最后把保存编辑的.csproj文件,然后点击重新加载项目,即可迁移到.net5.0.

作者:「一点几_ZK」