我剛剛設定了新專案Pegasus(我的主要 Web 應用程式)和PegasusEntities(我的資料庫物件)。
我的Pegasus啟動代碼包括以下內容。
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(
builder.Configuration.GetConnectionString(connectionString),
builder => builder.MigrationsAssembly("PegasusEntities"));
});
如果我在包管理器控制臺中將Pegasus設定為我的默認專案并嘗試添加遷移,則會收到錯誤訊息。
PM> add-migration CreateDatabase
Multiple startup projects set.
Using project 'Pegasus' as the startup project.
Build started...
Build succeeded.
Your target project 'Pegasus' doesn't match your migrations assembly 'PegasusEntities'. Either change your target project or change your migrations assembly.
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("Pegasus")). By default, the migrations assembly is the assembly containing the DbContext.
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.
我不明白這個問題。專案在哪些方面“不匹配”?為什么要我將遷移程式集設定為Pegasus?我在另一個專案中做同樣的事情,它運行良好。
有人可以解釋為什么這不起作用嗎?
更新
盡管我在另一個專案中做同樣的事情并且作業正常,但我嘗試將包管理器控制臺中的默認專案設定為PegasusEntities。當我這樣做時,我得到一個完全不同的錯誤。
PM> add-migration CreateDatabase
Multiple startup projects set.
Using project 'PegasusEntities' as the startup project.
Build started...
Build succeeded.
It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '2.0.0' (x64) was not found.
- The following frameworks were found:
5.0.12 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
5.0.16 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
6.0.4 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
You can resolve the problem by installing the specified framework and/or SDK.
The specified framework can be found at:
- https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=2.0.0&arch=x64&rid=win10-x64
這對我來說毫無意義。所有專案都針對net6.0. 為什么要尋找版本 2 Microsoft.NETCore.App?當我打開建議的鏈接時,我得到一個頁面,上面寫著此版本已結束生命,這意味著它不再受支持。我們建議遷移到受支持的版本,例如 7.0 Runtime。.
uj5u.com熱心網友回復:
我昨天才遇到同樣的問題。
該錯誤訊息非常具有誤導性,即使不是完全不正確。
解決方法是將除錯啟動專案設定為實體化 DbContext 的專案,同時將專案與包管理器控制臺中選擇的物體保持一致。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/474283.html
標籤:C# 网 ef-code-first 实体框架迁移 .net-6.0
