EFCore .net6 Cosmos 迁移问题

发布于 2025-01-20 02:27:41 字数 1634 浏览 0 评论 0原文

.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.

我的 Program.cs:

builder.Services.AddDbContext<ControlCenterContext>(options =>
options.UseCosmos(builder.Configuration.GetConnectionString("DefaultConnection"), "ToDoList"));

我的 DbContext Impl:

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" 时,我收到上面提到的错误。

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

.NET6 EFCore & Cosmos Migration issue. Need some help.

Hello folks. I am new in the world of .Net and I am facing an issue that Google has failed to help me solve. You're kind of my last regard.

So. I am trying to connect to an Azure Cosmos DB from my little HomeControlCenter Project using EFCore 6.0.3

The Error:

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.

My Program.cs:

builder.Services.AddDbContext<ControlCenterContext>(options =>
options.UseCosmos(builder.Configuration.GetConnectionString("DefaultConnection"), "ToDoList"));

My DbContext Impl:

public class ControlCenterContext : DbContext
{
    public ControlCenterContext(DbContextOptions<ControlCenterContext> options) : base(options)
    {
        
    }
}

I also tried to use an override of OnConfiguring instead of the Program.cs line.

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseCosmos(
            "<AccountEndpoint>",
            "<accountKey>",
            databaseName: "ToDoList");
    

Nothing helped. When ever I run dotnet ef migrations add "initialSetup" I get the error mentioned above.

I read the error carefully and as you can see, I did apply all the necessary constructor params & other additions... I even tried to create a vanilla project and do the same all over again...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2025-01-27 02:27:41

I couldn't find anything official from Microsoft, but the author of this blog states migrations using EF Core for CosmosDb are not supported:

这是有道理的文档数据库,因此没有架构,它只是一堆JSON文件。当我想使用迁移来制作种子数据时,我遇到了这个问题。我唯一能想到的解决方案是创建一个单独的项目,该项目以静态值上传了种子数据。但同样,这只是种子数据,而不是架构更新。

I couldn't find anything official from Microsoft, but the author of this blog states migrations using EF Core for CosmosDb are not supported: https://www.thereformedprogrammer.net/an-in-depth-study-of-cosmos-db-and-ef-core-3-0-database-provider/#1-no-migrations-can-cause-problems

This makes sense since CosmosDB is a document database, so it has no schema, it's just a bunch of JSON files. I ran into this issue when I wanted to use migrations to make seed data. The only solution I could think of was to create a separate project that uploaded the seed data with static values. But again, this was only seed data and not schema updates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文