从另一个项目为ApiAuthorizationDbContext添加迁移-EF Core [英] Add migration for ApiAuthorizationDbContext from another project - EF Core
问题描述
我正在尝试从另一个.NET Core项目为 ApiAuthorizationDbContext
添加迁移,但是由于我不知道如何获取第二个参数 IOptions< OperationalStoreOptions> ;,因此无法从设计时实例化它;
.
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>
.
这是我的DbContext构造函数(继承了我的自定义 ApiAuthorizationDbContext
,它接受TUser,TRole和TKey)
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)
{
}
这是我的 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 ??
}
}
我从此问题在GitHub中找到了答案,但仍然无法确定找出解决该参数的方法.
I found an answer from this issue in GitHub but still can not figure out a way how to resolve this param.
我也尝试将IOptions<>注入构造函数,但是在添加迁移时,它抛出一个异常,即找不到DesignTimeDbContextFactory的无参数构造函数
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
有人可以给我一个提示吗,我对.NET Core/EF Core还是很陌生,如果有人可以提供帮助,她将非常有帮助!
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 3.0和Entity Framework Core)
(I'm using .NET Core 3.0 and Entity Framework Core)
推荐答案
继承 IOptions 创建 OperationalStoreOptionsMigrations ,并传递 OperationalStoreOptionsMigrations 对象.看看我的答案.
Create OperationalStoreOptionsMigrations inheriting the IOptions and pass OperationalStoreOptionsMigrations object. See my answer.
-
创建 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,
};
}
更改 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());
}
}
这篇关于从另一个项目为ApiAuthorizationDbContext添加迁移-EF Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!