升級到 .NET Core 2.1

從 .NET Core 2.0 升級到 .NET Core 2.1

最近在翻譯 Pro ASP.NET Core MVC 2這本書,書中的示例是以 .NET Core 2.0 爲基礎的,.NET Core 2.1 已經發布了,在你安裝了當前的 .NET Core 2.1 以後,雖然核心的代碼保持了一致性,可是,部分代碼仍是會發現沒法與書中保持一致,這裏列出從 .NET Core 2.0 升級到 .NET Core 2.1 的關鍵點,供讀者參考。git

.NET Core 2.0 中的項目文件

在 .NET Core 2.0 時代,項目文件中的 TargetFramework 設置爲 netcoreapp2.0,應用必須引用的庫爲 Microsoft.AspNetCore.Allgithub

若是咱們還須要使用 Entity Framework 的工具,還須要添加工具引用。sql

<Project Sdk="Microsoft.NET.Sdk.Web">
   <PropertyGroup>
      <TargetFramework>netcoreapp2.0</TargetFramework>
   </PropertyGroup><ItemGroup>
      <Folder Include="wwwroot\" />
   </ItemGroup>
    
   <ItemGroup>
      <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
      <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
   </ItemGroup>
</Project>

可是,隨着 .NET Core 2.1 的發佈,微軟已經建議再也不使用 Microsoft.AspNetCore.All 這個包。json

We recommend applications targeting ASP.NET Core 2.1 and later use the Microsoft.AspNetCore.App rather than this package. See Migrating from Microsoft.AspNetCore.All to Microsoft.AspNetCore.App in this article.app

對於面向 ASP.NET Core 2.1 及更高版本的應用程序,建議使用 Microsoft.AspNetCore.App 而不是此包。見文內的 從 Microsoft.AspNetCore.All 遷移到 Microsoft.AspNetCore.App框架

來源:Microsoft.AspNetCore.All metapackage for ASP.NET Core 2.0工具

 

關於 Microsoft.AspNetCore.App

.NET Core 2.1 中,項目文件中的TargetFramework 設置爲 netcoreapp2.1,而引用的包則變成 Microsoft.AspNetCore.Appui

<Project Sdk="Microsoft.NET.Sdk.Web"><PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup><ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup></Project>

並且,Microsoft.EntityFrameworkCore.Tools.DotNet 已經包含於 Microsoft.AspNetCore.App 以內,再也不須要單獨安裝。this

The .NET Core SDK version 2.1.300 and newer includes dotnet ef commands that are compatible with EF Core 2.0 and later versions. Therefore if you are using recent versions of the .NET Core SDK and the EF Core runtime, no installation is required and you can ignore the rest of this section.spa

On the other hand, the dotnet ef tool contained in .NET Core SDK version 2.1.300 and newer is not compatible with EF Core version 1.0 and 1.1. Before you can work with a project that uses these earlier versions of EF Core on a computer that has .NET Core SDK 2.1.300 or newer installed, you must also install version 2.1.200 or older of the SDK and configure the application to use that older version by modifying its global.json file. This file is normally included in the solution directory (one above the project). Then you can proceed with the installlation instruction below.

.NET Core SDK version 2.1.300 及更新版本包含了兼容 EF Core 2.0 及更新版本的 dotnet ef 命令行工具。所以,若是您使用最新版本的 .NET Core SDK 和 EF Core 運行時,將再也不須要安裝此工具,您能夠忽略剩下的步驟。

從另外一個方面來講,包含於 .NET Core SDK version 2.1.300 及更新版本的 dotnet ef 工具不兼容 EF Core 1.0 和 1.1。在您於安裝了 .NET Core SDK 2.1.300 或更新版本的計算機上,使用這些包含早期版本的 EF Core 項目以前,您必須還要安裝 2.1.200 或者更早的 SDK ,並經過修改項目的 global.json 來配置應用程序使用早期版本。該文件一般位於解決方案文件夾中(項目的上一級)。而後能夠按照以下安裝指導處理。

來源:EF Core .NET Command-line Tools

 

從 Microsoft.AspNetCore.All 遷移到 Microsoft.AspNetCore.App

下面所列出的內容包含於 Microsoft.AspNetCore.All 但不包含於 Microsoft.AspNetCore.App 包內。

  • Microsoft.AspNetCore.ApplicationInsights.HostingStartup

  • Microsoft.AspNetCore.AzureAppServices.HostingStartup

  • Microsoft.AspNetCore.AzureAppServicesIntegration

  • Microsoft.AspNetCore.DataProtection.AzureKeyVault

  • Microsoft.AspNetCore.DataProtection.AzureStorage

  • Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv

  • Microsoft.AspNetCore.SignalR.Redis

  • Microsoft.Data.Sqlite

  • Microsoft.Data.Sqlite.Core

  • Microsoft.EntityFrameworkCore.Sqlite

  • Microsoft.EntityFrameworkCore.Sqlite.Core

  • Microsoft.Extensions.Caching.Redis

  • Microsoft.Extensions.Configuration.AzureKeyVault

  • Microsoft.Extensions.Logging.AzureAppServices

  • Microsoft.VisualStudio.Web.BrowserLink

在從 Microsoft.AspNetCore.All 遷移到 Microsoft.AspNetCore.App 時,若是您的應用使用了來自上述包或者這些包所引入的包中所提供的 API,在您的項目中添加其引用。

不會隱式包含非 Microsoft.AspNetCore.App 依賴項的上述包的任何依賴項。 例如:

  • StackExchange.Redis 做爲 Microsoft.Extensions.Caching.Redis的依賴

  • Microsoft.ApplicationInsights 做爲 Microsoft.AspNetCore.ApplicationInsights.HostingStartup 的依賴

從 .NET Core 2.0 遷移到 .NET Core 2.1

  • 打開項目文件 ( *.csproj, *.vbproj, 或者 *.fsproj 文件)。

  • target framework 值從 netcoreapp2.0 修改成 netcoreapp2.1。目標框架由元素 <TargetFramework> 或者 <TargetFrameworks> 定義。

    例如,將 <TargetFramework>netcoreapp2.0</TargetFramework> 修改成<TargetFramework>netcoreapp2.1</TargetFramework>

  • 刪除適用於 .NET Core 2.1 SDK (v 2.1.300 及更新版本) 中捆綁的工具的 <DotNetCliToolReference> 引用。這些引用包括:

    在從前的 .NET Core SDK 版本中,項目中的這些工具引用以下所示:

    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />

    因爲這些條目再也不被 .NET Core SDK 所使用,若是您仍舊在項目中使用了這些打包的工具,您將看到相似以下所示的警告:

    The tool 'Microsoft.EntityFrameworkCore.Tools.DotNet' is now included in the .NET Core SDK. Here is information on resolving this warning.

    從項目文件的 <DotNetCliToolReference> 中刪除這些引用能夠修復該問題。

來源:Update the project file to use 2.1 versions

相關文章
相關標籤/搜索