![](/img/trans.png)
[英].NET 5 and Entity Framework Core (5.0.1) migration issue with Cosmos
[英]EFCore .net6 Cosmos migration issue
.NET6 EFCore 和 Cosmos 遷移問題。 需要一些幫助。
大家好。 我是 .Net 世界的新手,我正面臨 Google 未能幫助我解決的問題。 你是我最后的敬意。
所以。 我正在嘗試使用 EFCore 6.0.3 從我的 HomeControlCenter 小項目連接到 Azure Cosmos DB
錯誤:
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Migrations.IMigrator'. This is often because no database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext>
object in its constructor and passes it to the base constructor for DbContext.
我的程序.cs:
builder.Services.AddDbContext<ControlCenterContext>(options =>
options.UseCosmos(builder.Configuration.GetConnectionString("DefaultConnection"), "ToDoList"));
我的 DbContext 實現:
public class ControlCenterContext : DbContext
{
public ControlCenterContext(DbContextOptions<ControlCenterContext> options) : base(options)
{
}
}
我還嘗試使用 OnConfiguring 的替代而不是 Program.cs 行。
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseCosmos(
"<AccountEndpoint>",
"<accountKey>",
databaseName: "ToDoList");
沒有任何幫助。 每當我運行do.net ef migrations add "initialSetup"
時,我都會收到上述錯誤。
我仔細閱讀了錯誤,正如您所看到的,我確實應用了所有必要的構造函數參數和其他添加項……我什至嘗試創建一個原始項目並再次執行相同的操作……
問題有點無厘頭。 Cosmos 是無模式的,因此可能不支持也不需要遷移。
我找不到任何來自 Microsoft 的官方信息,但此博客的作者指出不支持使用 EF Core for CosmosDb 進行遷移: https ://www.thereformedprogrammer.net/an-in-depth-study-of-cosmos-db -and-ef-core-3-0-database-provider/#1-no-migrations-can-cause-problems
這是有道理的,因為 CosmosDB 是一個文檔數據庫,所以它沒有模式,它只是一堆 JSON 文件。 當我想使用遷移來制作種子數據時,我遇到了這個問題。 我能想到的唯一解決方案是創建一個單獨的項目,上傳帶有靜態值的種子數據。 但同樣,這只是種子數據,而不是模式更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.