Open Closed

Exception while executing dotnet run --migrate-database #4508


User avatar
0
Sraman created
  • ABP Framework version: v7.0.1

  • UI type: MVC

  • DB provider: EF Core

  • Tiered (MVC) or Identity Server Separated (Angular): no (single-layer)

  • Exception message and stack trace: D:\Source\Repos\Pentapack.MRS\Pentapack.MRS>dotnet run --migrate-database [07:48:15 FTL] Pentapack.MRS terminated unexpectedly! Autofac.Core.DependencyResolutionException: An exception was thrown while activating Pentapack.MRS.Data.MRSDbMigrationService. ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Pentapack.MRS.Data.MRSDbMigrationService' can be invoked with the available services and parameters: Cannot resolve parameter 'Volo.Saas.Tenants.ITenantRepository tenantRepository' of constructor 'Void .ctor(Volo.Abp.Data.IDataSeeder, Pentapack.MRS.Data.MRSEFCoreDbSchemaMigrator, Volo.Saas.Tenants.ITenantRepository, Volo.Abp.MultiTenancy.ICurrentTenant)'. at Autofac.Core.Activators.Reflection.ReflectionActivator.<>c__DisplayClass12_0.<UseSingleConstructorActivation>b__0(ResolveRequestContext ctxt, Action1 next) at Autofac.Core.Resolving.Middleware.DelegateMiddleware.Execute(ResolveRequestContext context, Action1 next) at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) at Autofac.Core.Resolving.Middleware.DisposalTrackingMiddleware.Execute(ResolveRequestContext context, Action1 next) at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) at Autofac.Builder.RegistrationBuilder3.<>c__DisplayClass41_0.<PropertiesAutowired>b__0(ResolveRequestContext ctxt, Action1 next) at Autofac.Core.Resolving.Middleware.DelegateMiddleware.Execute(ResolveRequestContext context, Action1 next) at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) at Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action1 next) --- End of inner exception stack trace --- at Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action1 next) at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) at Autofac.Core.Pipeline.ResolvePipeline.Invoke(ResolveRequestContext ctxt) at Autofac.Core.Resolving.Middleware.RegistrationPipelineInvokeMiddleware.Execute(ResolveRequestContext context, Action1 next) at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) at Autofac.Core.Resolving.Middleware.SharingMiddleware.Execute(ResolveRequestContext context, Action1 next) at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) at Autofac.Core.Resolving.Middleware.CircularDependencyDetectorMiddleware.Execute(ResolveRequestContext context, Action1 next) at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) at Autofac.Core.Pipeline.ResolvePipeline.Invoke(ResolveRequestContext ctxt) at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest request) at Autofac.Core.Resolving.ResolveOperation.ExecuteOperation(ResolveRequest request) at Autofac.Core.Resolving.ResolveOperation.Execute(ResolveRequest request) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(ResolveRequest request) at Autofac.Core.Container.ResolveComponent(ResolveRequest request) at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable1 parameters) at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable1 parameters) at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType) at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetRequiredService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Pentapack.MRS.Program.Main(String[] args) in D:\Source\Repos\Pentapack.MRS\Pentapack.MRS\Program.cs:line 46

  • Steps to reproduce the issue:" Executing dotnet run --migrate-database


13 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I could not reproduce the problem, could you share the full steps to reproduce it? thanks.

  • User Avatar
    0
    Sraman created

    Hi, Even I tried with a new project and it works fine. But my existing project throws such exception and not sure why does that happens. Any idea what could be the issue?

  • User Avatar
    0
    Sraman created

    And also this is another exception I'm facing while executing Add-Migration command.

    PM> Add-Migration 'Initial' Build started... Build succeeded. [08:20:54 FTL] Pentapack.MRS terminated unexpectedly! Microsoft.Extensions.Hosting.HostAbortedException: The host was aborted. at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.ThrowHostAborted() at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.OnNext(KeyValuePair`2 value) at System.Diagnostics.DiagnosticListener.Write(String name, Object value) at Microsoft.Extensions.Hosting.HostBuilder.ResolveHost(IServiceProvider serviceProvider, DiagnosticListener diagnosticListener) at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build() at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build() at Pentapack.MRS.Program.Main(String[] args) in D:\Source\Repos\Pentapack.MRS\Pentapack.MRS\Program.cs:line 41

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    It's a DI problem, looks like one of your services is not registered with the container.

    Some possible reasons:

    • You didn't add the DbSet to the database context for your entity.
    • You miss the Dependency Interfaces for your own service

    Dependency Injection document: https://docs.abp.io/en/abp/latest/Dependency-Injection

    You can share the project with me if you still not found the problem, I will check it. shiwei.liang@volosoft.com

  • User Avatar
    0
    Sraman created

    Hi,

    I could resolve this exception "Microsoft.Extensions.Hosting.HostAbortedException". But couldn't find any clue on "Autofac.Core.DependencyResolutionException: An exception was thrown while activating Pentapack.MRS.Data.MRSDbMigrationService."

  • User Avatar
    0
    Sraman created

    Can you please check you mail box

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Please add these module dependencies to MRSModule

    // Saas Management module packages
    typeof(SaasHostWebModule),
    typeof(SaasHostHttpApiModule),
    typeof(SaasHostApplicationModule),
    typeof(SaasEntityFrameworkCoreModule)
    

    BTW, I guess you don't want to include the SaasModule in your project. if so, you don't need to add module dependencies. but you should remove all ITenantRepository uses.

    Update the MRSDbMigrationService class.

    public class MRSDbMigrationService : ITransientDependency
    {
        public ILogger<MRSDbMigrationService> Logger { get; set; }
    
        private readonly IDataSeeder _dataSeeder;
        private readonly MRSEFCoreDbSchemaMigrator _dbSchemaMigrator;
    
        public MRSDbMigrationService(
            IDataSeeder dataSeeder,
            MRSEFCoreDbSchemaMigrator dbSchemaMigrator)
        {
            _dataSeeder = dataSeeder;
            _dbSchemaMigrator = dbSchemaMigrator;
    
            Logger = NullLogger<MRSDbMigrationService>.Instance;
        }
    
        public async Task MigrateAsync()
        {
            var initialMigrationAdded = AddInitialMigrationIfNotExist();
    
            if (initialMigrationAdded)
            {
                return;
            }
    
            Logger.LogInformation("Started database migrations...");
    
            await MigrateDatabaseSchemaAsync();
            await SeedDataAsync();
    
            Logger.LogInformation($"Successfully completed host database migrations.");
    
            Logger.LogInformation("You can safely end this process...");
        }
        
        .........
     }
    

    Add the following code to Program class

    if (IsMigrateDatabase(args))
    {
        builder.Services.AddDataMigrationEnvironment();
    }
    

  • User Avatar
    0
    Sraman created

    Hi, Thanks it fixed that issue and now I'm able to migrate.

    But I have one issue while migration, that is I'm trying to create db and tables with default data on deployment by implementing the below code on OnApplicationInitialization in module file,

    var dbContext = serviceScope.ServiceProvider.GetRequiredService<MRSDbContext>(); dbContext.Database.EnsureCreated();

    It is creating db and their respective tables, but data seeding is not happening. User table looks empty.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You need to use the data seeding system to initial data: https://docs.abp.io/en/abp/latest/Data-Seeding

  • User Avatar
    0
    Sraman created

    I have implemented data seeding using IDataSeedContributor and it is working fine with ./migrate-database.

    But I'm looking to migrate on runtime while deploying using the below code,

    var dbContext = serviceScope.ServiceProvider.GetRequiredService<MRSDbContext>(); dbContext.Database.EnsureCreated();

    It is creating db and table, but not data.

    Any suggestions on this?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Try await serviceScope.ServiceProvider.GetRequiredService<MRSDbMigrationService>().MigrateAsync();

  • User Avatar
    0
    Sraman created

    That worked. But after deployment post requests are not working. Throwing below error.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I think your main problem has been solved and the new problem is not related to this.

    Could you create a new question? thanks.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 10, 2024, 06:38