Mike Lenart
Mike Lenart

Reputation: 939

.NET 5 and Entity Framework Core (5.0.1) migration issue with Cosmos

I have a console app (.NET 5) using Entity Framework Core 5.0.1 against an Azure Cosmos database.

I have the following for my DbContext:

public class DomainEventStoreContext : DbContext
{
    public DbSet<DomainEventStoreEntry> DomainEventLogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.UseCosmos(
                "https://abc-cosmodb.documents.azure.com:443/",
                "KeyString",
                databaseName: "DBName");

    protected override void OnModelCreating(ModelBuilder builder)
    {
        // the container name
        builder.HasDefaultContainer("DomainEvents");

        builder.Entity<DomainEventStoreEntry>()
            .ToContainer("DomainEvents");

        builder.Entity<DomainEventStoreEntry>()
            .HasNoDiscriminator();

        builder.Entity<DomainEventStoreEntry>()
            .HasNoKey();

        builder.Entity<DomainEventStoreEntry>()
            .HasPartitionKey(o => o.PartitionKey).Property(e => e.PartitionKey).IsRequired();

        builder.Entity<DomainEventStoreEntry>()
            .Property(e => e.EventId).IsRequired();

        builder.Entity<DomainEventStoreEntry>()
            .Property(e => e.Content).IsRequired();

        builder.Entity<DomainEventStoreEntry>()
            .Property(e => e.CreationTime).IsRequired();

        builder.Entity<DomainEventStoreEntry>()
            .Property(e => e.State).IsRequired();

        builder.Entity<DomainEventStoreEntry>()
            .Property(e => e.EventTypeName).IsRequired();

        builder.Entity<DomainEventStoreEntry>()
            .Property(e => e.TransactionId).IsRequired();

    }
}

I have the following code in Program.cs:

class Program
{
    static void Main(string[] args)
    {
        var context = new DomainEventStoreContext();
    }
}

When I try to create a migration I get the following 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 object in its constructor and passes it to the base constructor for DbContext."

I am using the baseline of Microsoft's Entity Framework for Cosmos on GitHub. Entity Framework for Cosmos Example on GitHub

Upvotes: 1

Views: 918

Answers (1)

Mike Lenart
Mike Lenart

Reputation: 939

I just found out that with Azure Cosmos it doesnt support migrations. You have to call context.Database.EnsureCreated() to ensure it is has been created.

Upvotes: 5

Related Questions