Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>.

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)

like image 771
viethieule Avatar asked Nov 23 '19 16:11

viethieule


People also ask

How do I add migration to ef 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.

How do you add migration to a specific context?

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.


1 Answers

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

  1. 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,
           };
     }
    
  2. 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()); 
            }
       }
    
like image 164
shalitha senanayaka Avatar answered Oct 16 '22 20:10

shalitha senanayaka