Starts in:
2 DAYS
9 HRS
21 MIN
27 SEC
Starts in:
2 D
9 H
21 M
27 S
Open Closed

DBMigrator with two DbContext #999


User avatar
0
Leonardo.Willrich created
  • ABP Framework version: v4.2.1
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Steps to reproduce the issue:

Hi,

I have two DBContext in my application and I want to know how to use DBMigrator to apply migration for both DBContext? To create (add-migration) and to update the database (update-database) I am using the argument -context <dbcontext>. But, how it will be possible using DBMigrator? I want to seed my database using DBMigrator.


5 Answer(s)
  • User Avatar
    0
    Leonardo.Willrich created

    Just as additional information, I've created a new MigrationService and implemented that. But, I still don't know how to distinguish the SeedContributor in each method. It is still calling the same class in both services.

    He is my new Service being instanced and calling MigrateAsync.

    public async Task StartAsync(CancellationToken cancellationToken)
            {
                using (var application = AbpApplicationFactory.Create<AvalancheOCPDbMigratorModule>(options =>
                    {
                        options.UseAutofac();
                        options.Services.AddLogging(c => c.AddSerilog());
                    }))
                    {
                        application.Initialize();
    
                        await application
                            .ServiceProvider
                            .GetRequiredService<AvalancheOCPDbMigrationService>()
                            .MigrateAsync();
    
                        await application
                            .ServiceProvider
                            .GetRequiredService<AORDbMigrationService>()
                            .MigrateAsync();
    
                    application.Shutdown();
    
                        _hostApplicationLifetime.StopApplication();
                    }
    
            }
    

    The problem is in the highlighted line. It is bringing all SeedContributor classes created.

    public AORDbMigrationService(
                IDataSeeder dataSeeder,
                IEnumerable<IAORDbSchemaMigrator> dbSchemaMigrators,
                ITenantRepository tenantRepository,
                ICurrentTenant currentTenant)
            {
                _dataSeeder = dataSeeder; // -->> Return all list with all SeedContributor, and not the only one that is suppose to be for the DBContext
                _dbSchemaMigrators = dbSchemaMigrators;
                _tenantRepository = tenantRepository;
                _currentTenant = currentTenant;
    
                Logger = NullLogger<AORDbMigrationService>.Instance;
            }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi Leonardo.Willrich

    _dataSeeder = dataSeeder; // -->> Return all list with all SeedContributor, and not the only one that is suppose to be for the DBContext

    In this case you need to manually resolve the instance of IDataSeedContributor

  • User Avatar
    0
    Leonardo.Willrich created

    Do you have any example of that? What do you mean by create manually? Should I change the service for the original DbContext? Is there some attribute for the class such as [DBContextAttribute.DbContext()]?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    In fact, you can ignore this problem, because IDataSeedContributor will determine whether the data exists before the seed data.

  • User Avatar
    0
    Leonardo.Willrich created

    Hi.

    I've sorted out the issue. The problem is that the both MigrationService were calling the method to seed the database. I have removed from the first service that was being called and left only in the latest one. Thus, after all tables created, it will run the seeds and if the table is empty then it will populate that as per the method implementation.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06