C
C#10mo 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
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>();
}
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>();
}
7 Replies
Pobiega
Pobiega10mo ago
Do you have di setup for this as well?
Tvde1
Tvde110mo ago
you might need to add which project has your DB context and which has your startup
Pobiega
Pobiega10mo ago
The migration builder will actually build your app configuration and service provider
Angius
Angius10mo ago
Seems like the config isn't hooked up properly
SWEETPONY
SWEETPONYOP10mo ago
I have two projects: A with DI setup B with dbcontext I want to create migrations in B that's why I set B as startup project and write this command: add-migration UpdateSomeModels -contex dbcontex but it doesn't work
Tvde1
Tvde110mo ago
you need to do
add-migration MyNewThings -p MyProject.Data/ -s MyProject.API
add-migration MyNewThings -p MyProject.Data/ -s MyProject.API
the context option shouldn't be used unless you have multiple DB contexts also I recommend to use the newer tool: dotnet ef migrations add ...`
SWEETPONY
SWEETPONYOP10mo ago
ahh I found the problem! we should set a class with DI as startup and create migration in project with dbcontext thanks
Want results from more Discord servers?
Add your server