.NET Core 2.0 中的項目文件
在 .NET Core 2.0 時代,項目文件中的 TargetFramework
設置為 netcoreapp2.0
,應用必須引用的庫為 Microsoft.AspNetCore.All
。
如果我們還需要使用 Entity Framework 的工具,還需要添加工具引用。
<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
這個包。
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.
對於面向 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.App
。
<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
之內,不再需要單獨安裝。
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.
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>
引用。這些引用包括:-
dotnet-watch (Microsoft.DotNet.Watcher.Tools)
-
dotnet-user-secrets (Microsoft.Extensions.SecretManager.Tools)
-
dotnet-sql-cache (Microsoft.Extensions.Caching.SqlConfig.Tools)
-
dotnet-ef (Microsoft.EntityFrameworkCore.Tools.DotNet)
在從前的 .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>
中刪除這些引用可以修復該問題。 -