C
C#5mo ago
Lisa

✅ Unable to create migration - 'Unable to resolve service for type 'DbContextOptions'

This is my first time creating migrations (never had a need until now), so please bear with me. I'm trying to use dotnet ef migrations add AMigrationName to add a migration to my project, However, I get this error:
Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOpt
ions`1[Treehouse_Guardian.Database.DatabaseContext]' while attempting to activate 'Treehouse_Guardian.Database.DatabaseContext'.' was th
rown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?l
inkid=851728
Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOpt
ions`1[Treehouse_Guardian.Database.DatabaseContext]' while attempting to activate 'Treehouse_Guardian.Database.DatabaseContext'.' was th
rown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?l
inkid=851728
My startup looks like this:
private Program()
{
_configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false)
.Build();

_serviceProvider = new ServiceCollection()
.AddSingleton(_configuration)
.AddSingleton(ConfigureDiscordSocketConfig)
.AddSingleton(ConfigureDiscordSocketClient)
.AddSingleton<InteractionService>()
.AddSingleton<InteractionHandler>()
.AddSingleton<WritingService.WritingService>()
.AddSingleton<TreehouseMemberService>()
.AddSingleton<BenzHandler>()
.AddSingleton<GniHandler>()
.AddSingleton<HttpClient>()
.AddDbContext<DatabaseContext>(ConfigureDbContext)
.BuildServiceProvider();

Logger.ConfigureLogging();
}
private Program()
{
_configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false)
.Build();

_serviceProvider = new ServiceCollection()
.AddSingleton(_configuration)
.AddSingleton(ConfigureDiscordSocketConfig)
.AddSingleton(ConfigureDiscordSocketClient)
.AddSingleton<InteractionService>()
.AddSingleton<InteractionHandler>()
.AddSingleton<WritingService.WritingService>()
.AddSingleton<TreehouseMemberService>()
.AddSingleton<BenzHandler>()
.AddSingleton<GniHandler>()
.AddSingleton<HttpClient>()
.AddDbContext<DatabaseContext>(ConfigureDbContext)
.BuildServiceProvider();

Logger.ConfigureLogging();
}
And ConfigureDbContext is just:
private void ConfigureDbContext(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(_configuration["Postgres:ConnectionString"]);
}
private void ConfigureDbContext(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(_configuration["Postgres:ConnectionString"]);
}
My databasecontext looks like this:
using Microsoft.EntityFrameworkCore;
using Treehouse_Guardian.Model;

namespace Treehouse_Guardian.Database;

public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbContext(options)
{
public DbSet<TreehouseMember> TreehouseMember
{
get;
set;
}
}
using Microsoft.EntityFrameworkCore;
using Treehouse_Guardian.Model;

namespace Treehouse_Guardian.Database;

public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbContext(options)
{
public DbSet<TreehouseMember> TreehouseMember
{
get;
set;
}
}
What am I missing? How can I make sure the migration creator can access my dbcontextoptions at design time?
1 Reply
Lisa
LisaOP5mo ago
Created a context factory, seems to work fine now:
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace Treehouse_Guardian.Database;

public class DatabaseContextFactory : IDesignTimeDbContextFactory<DatabaseContext>
{
public DatabaseContext CreateDbContext(string[] args)
{
var configuration = new ConfigurationManager()
.AddJsonFile("appsettings.json", false)
.Build();

var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
optionsBuilder.UseNpgsql(configuration["Postgres:ConnectionString"]);

return new DatabaseContext(optionsBuilder.Options);
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace Treehouse_Guardian.Database;

public class DatabaseContextFactory : IDesignTimeDbContextFactory<DatabaseContext>
{
public DatabaseContext CreateDbContext(string[] args)
{
var configuration = new ConfigurationManager()
.AddJsonFile("appsettings.json", false)
.Build();

var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
optionsBuilder.UseNpgsql(configuration["Postgres:ConnectionString"]);

return new DatabaseContext(optionsBuilder.Options);
}
}
Want results from more Discord servers?
Add your server