EFCore .net6 Cosmos migration issue

Viewed 289

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

2 Answers

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.

Question is a bit nonsensical. Cosmos is Schema-less, thus a migration is likely not supported nor needed anyways.

Related