c# - EF Core .net 6 Cosmos 迁移问题

标签 c# .net entity-framework azure-cosmosdb

.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");
    

没有任何帮助。当我运行 dotnet ef migrations add "initialSetup" 时,我收到上面提到的错误。

我仔细阅读了错误,正如您所看到的,我确实应用了所有必要的构造函数参数和其他添加内容...我什至尝试创建一个普通项目并再次执行相同的操作...

最佳答案

我找不到 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 文件。当我想使用迁移来制作种子数据时,我遇到了这个问题。我能想到的唯一解决方案是创建一个单独的项目,上传具有静态值的种子数据。但同样,这只是种子数据,而不是架构更新。

关于c# - EF Core .net 6 Cosmos 迁移问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71795255/

相关文章:

c# - 检测明亮和黑暗的图像

c# - 提交表单时如何获取当前 View 的操作名称?

c# - 将winform定位在屏幕的左下角

c# - 代码在 Release模式和 Debug模式下的行为不同

c# - ASP.Net Display DateTime 中的图表助手

c# - .net StackFrame 和当前行/列

sql - 在Entity Framework 1.0中SaveChanges之后触发存储过程

c# - 为什么在 IIS 上的网站下作为应用程序托管时 swagger.json 找不到错误?

c# - 带有 Power Tools 3.x Beta 的 EF5.x

asp.net-mvc - 无法播种用户和角色