C#C
C#2y ago
SWEETPONY

✅ How to create migration correctly? What do I do wrong?

Command in Package Manager Console:
PM> add-migration UpdateSomeModels -contex ReadModelDbContext
Error:
Unable to create a 'DbContext' of type 'ReadModelDbContext'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[ReadModel.Infrastructure.ReadModelDbContext]' while attempting to activate 'ReadModel.Infrastructure.ReadModelDbContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

public sealed class ReadModelDbContext: DbContext
{
    public ReadModelDbContext(DbContextOptions<ReadModelDbContext> options)
        : base(options)
    {
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Qualification>().HasAlternateKey(x => x.Identity);
        modelBuilder.Entity<Function>().HasIndex(x => x.Name);
        modelBuilder.Entity<Resource>().HasIndex(x => x.Identity);
        modelBuilder.Entity<Node>().HasIndex(x => x.Identity);
        modelBuilder.Entity<Location>().HasIndex(x => x.Identity);
        modelBuilder.Entity<Shift>().HasAlternateKey(x => new { x.ScheduledStart, x.Type, x.ResourceId });
    }

    public DbSet<Qualification> Qualifications => Set<Qualification>();
    public DbSet<Function> Functions => Set<Function>();
    public DbSet<FunctionQualification> FunctionQualifications => Set<FunctionQualification>();
    public DbSet<ResourceQualification> ResourceQualifications => Set<ResourceQualification>();
    public DbSet<Resource> Resources => Set<Resource>();
    public DbSet<Node> Nodes => Set<Node>();
    public DbSet<Location> Locations => Set<Location>();
    public DbSet<Edge> Edges => Set<Edge>();
    public DbSet<ShiftQualification> ShiftQualifications => Set<ShiftQualification>();
    public DbSet<Shift> Shifts => Set<Shift>();
}
Was this page helpful?