I'm trying to add migration for ApiAuthorizationDbContext
from another .NET Core project but cannot instantiate it from design time since I don't know how to get the 2nd parameter IOptions<OperationalStoreOptions>
.
This is my DbContext constructor (which inherits my custom ApiAuthorizationDbContext
that accepts TUser, TRole, TKey)
public class ApplicationDbContext : ApiAuthorizationDbContext<ApplicationUser, ApplicationRole, Guid>
{
public ApplicationDbContext (DbContextOptions options, IOptions<OperationalStoreOptions> operationalStoreOptions)
: base(options, operationalStoreOptions)
{
}
This is my DesignTimeDbContextFactory
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<KontestDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").Build();
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
var connectionString = configuration.GetConnectionString("DefaultConnection");
builder.UseSqlServer(connectionString);
return new ApplicationDbContext(builder.Options, ????); <--- how to resolve the IOptions<OperationStoreOptions> here ??
}
}
I found an answer from this issue in GitHub but still can not figure out a way how to resolve this param.
I also tried to inject the IOptions<> to the constructor but when add-migration, it throws an exception that the parameterless constructor of DesignTimeDbContextFactory is not found
Can somebody give me a hint through this, I'm quite new to .NET Core / EF Core here and will very much appericiate if someone can help!
(I'm using .NET Core 3.0 and Entity Framework Core)
Adding a Migration So, firstly, you need to create a migration. Open the Package Manager Console from the menu Tools -> NuGet Package Manager -> Package Manager Console in Visual Studio and execute the following command to add a migration.
One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.
Create OperationalStoreOptionsMigrations inheriting the IOptions and pass OperationalStoreOptionsMigrations object. See my answer.
Create OperationalStoreOptionsMigrations method.
public class OperationalStoreOptionsMigrations :
IOptions<OperationalStoreOptions>
{
public OperationalStoreOptions Value => new OperationalStoreOptions()
{
DeviceFlowCodes = new TableConfiguration("DeviceCodes"),
EnableTokenCleanup = false,
PersistedGrants = new TableConfiguration("PersistedGrants"),
TokenCleanupBatchSize = 100,
TokenCleanupInterval = 3600,
};
}
change DesignTimeDbContextFactory
public class DesignTimeDbContextFactory :
IDesignTimeDbContextFactory<KontestDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").Build();
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
var connectionString =
configuration.GetConnectionString("DefaultConnection");
builder.UseSqlServer(connectionString);
return new ApplicationDbContext(builder.Options, new OperationalStoreOptionsMigrations());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With