从另一个项目添加对 ApiAuthorizationDbContext 的迁移 - EF Core [英] Add migration for ApiAuthorizationDbContext from another project - EF Core

查看:31
本文介绍了从另一个项目添加对 ApiAuthorizationDbContext 的迁移 - 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)

推荐答案

创建 OperationalStoreOptionsMigrations 继承 IOptions 并传递 OperationalStoreOptionsMigrations 对象.看我的回答.

Create OperationalStoreOptionsMigrations inheriting the IOptions and pass OperationalStoreOptionsMigrations object. See my answer.

  1. 创建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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆