一.NET core升級3.1
.NET Core 3.1 作為LTS長期支持版本,會提供3年的支持(明年就出.net5),值得升級(嗎)。
目前主流的第三方包大多都已經提供了支持,2.x => 3.1還是變化不是特別多,EF Core坑就大咯,謹慎。
ASP.NET Core 3.1 的新增功能
https://docs.microsoft.com/zh-cn/aspnet/core/release-notes/aspnetcore-3.1?view=aspnetcore-3.1
EF Core 3.0重大改變
https://docs.microsoft.com/zh-cn/ef/core/what-is-new/ef-core-3.0/breaking-changes
1,將VS2019更新到16.4.x,會自動安裝3.1的SDK。
2,將項目目標框架升級到3.1,右鍵項目 - 屬性 - 目標框架,或者修改csproj文件。
<PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> </PropertyGroup>
3,WEB項目中Program.cs文件,WebHost 改為 Host
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
4,WEB項目中Startup.cs
ConfigureServices 方法中 services.AddMvc() 改為 services.AddControllersWithViews()
//services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddControllersWithViews();
Configure 方法中 app.UseMvc() 改為 app.UseRouting() 與 app.UseEndpoints()
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseAuthorization(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }
5,更新所有(相關)nuget包到最新
6,重新生成解決方案,報錯 The project .... must provide a value for Configuration.
從 Web 項目中移除 Microsoft.AspNetCore... 的 PackageReference,因為已經包含在3.1的目標框架內了
<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
大功告成,至少編譯通過了,運行中目前只碰到一個坑。
A tracking query projects owned entity without corresponding owner in result.Owned entities cannot be tracked without their owner. Either include the owner entity in the result or make query non-tracking using AsNoTracking().
AutoMapper的ProjectTo<>導致,暫時加了個AsNoTracking()解決問題,根據報錯信息來看還有另一種解決方案。
public IEnumerable<UserDto> GetAll() { return _userRepository.GetAll(tracking: false) .OrderByDescending(s => s.Id) .ProjectTo<UserDto>(_mapper.ConfigurationProvider) .AsEnumerable(); }
二.ABP框架升級
注意問題
1.json序列化反序列 由以前的 Newtonsoft.Json 變更為 .net core3.1 自帶的 System.Text.Json
2.ABP框架2.6 Entity 實體 Id屬性更改為 只有get屬性 所以添加的時候需要業務自己傳入Id
MapToEntity 對象轉化方法自動填充主鍵Id功能取消 SetIdForGuids 方法刪除
3.BackgroundJobsDomainModule 變成 AbpBackgroundJobsDomainModule
4.BackgroundJobsEntityFrameworkCoreModule 變成 AbpBackgroundJobsEntityFrameworkCoreModule
5.UnitOfWorkOptions 變成 AbpUnitOfWorkOptions
6.Volo.Abp.Validation 單獨成一個模塊
7.AutoMapFrom 移除 使用 Profile CreateMap<DTO, Entity>(); 方式
8.異步方法變更
GetList------------->GetListAsync
Delete------------->DeleteAsync
Complete------------->CompleteAsync
Get------------->GetAsync
Find------------->FindAsync
Insert------------->InsertAsync
Update------------->UpdateAsync
9.如果程序集界面無法升級 可以直接編輯 csproj 文件
示例
<TargetFramework>netcoreapp3.1</TargetFramework> <ItemGroup> <PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.3" /> <PackageReference Include="Serilog.AspNetCore" Version="3.2.0" /> <PackageReference Include="Serilog.Sinks.Async" Version="1.4.0" /> <PackageReference Include="Serilog.Sinks.File" Version="4.1.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.3.1" /> <PackageReference Include="Volo.Abp.AspNetCore.Mvc" Version="2.6.0" /> <PackageReference Include="Volo.Abp.AspNetCore.Mvc.UI.Bootstrap" Version="2.6.0" /> <PackageReference Include="Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic" Version="2.6.0" /> <PackageReference Include="Volo.Abp.Autofac" Version="2.6.0" /> </ItemGroup>
其他第三方包統一根據具體情況而定(不一定要升級最新包 需要跟ABP依賴包對比)
10.程序啟動中間件變更
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseMvcWithDefaultRouteAndArea();