[英]Add migration for ApiAuthorizationDbContext from another project - EF Core
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>
.我正在尝试从另一个 .NET Core 项目为
ApiAuthorizationDbContext
添加迁移,但无法从设计时实例化它,因为我不知道如何获取第二个参数IOptions<OperationalStoreOptions>
。
This is my DbContext constructor (which inherits my custom ApiAuthorizationDbContext
that accepts TUser, TRole, TKey)这是我的 DbContext 构造函数(它继承了我接受 TUser、TRole、TKey 的自定义
ApiAuthorizationDbContext
)
public class ApplicationDbContext : ApiAuthorizationDbContext<ApplicationUser, ApplicationRole, Guid>
{
public ApplicationDbContext (DbContextOptions options, IOptions<OperationalStoreOptions> operationalStoreOptions)
: base(options, operationalStoreOptions)
{
}
This is my DesignTimeDbContextFactory
这是我的
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.我在 GitHub 中找到了这个问题的答案,但仍然无法找到解决此参数的方法。
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我也尝试将 IOptions<> 注入构造函数,但是在添加迁移时,它抛出一个异常,即找不到 DesignTimeDbContextFactory 的无参数构造函数
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!有人可以通过这个给我一个提示,我对 .NET Core / EF Core 很陌生,如果有人可以提供帮助,我会非常高兴!
(I'm using .NET Core 3.0 and Entity Framework Core) (我使用 .NET Core 3.0 和 Entity Framework Core)
Create OperationalStoreOptionsMigrations inheriting the IOptions and pass OperationalStoreOptionsMigrations object.创建OperationalStoreOptionsMigrations继承IOptions并通过OperationalStoreOptionsMigrations对象。 See my answer.
看我的回答。
Create OperationalStoreOptionsMigrations method.创建OperationalStoreOptionsMigrations方法。
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更改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()); } }
Pass your IOptions<OprerationalStoreOption>
as a parameter in the constructor then you can use it wherever you need to.将您的
IOptions<OprerationalStoreOption>
作为构造函数中的参数传递,然后您可以在任何需要的地方使用它。 Then register the IOptions
service in the DI
container.然后在
DI
容器中注册IOptions
服务。 Something like this像这样的东西
public ApplicationDbContext CreateDbContext(string[] args, Options<OperationStoreOptions> optionsBuilder)
{
IConfiguration configuration = new ConfigurationBuilder()
return new ApplicationDbContext(builder.Options, optionsBuilder);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.