Open Closed

Regarding Unit test case issue #1851


User avatar
0
ChetanKumbhar created

I am getting this error after running unit test cases. Volo.Abp.AbpInitializationException : An error occurred during ConfigureServices phase of the module *******************.EntityFrameworkCore.*******************tEntityFrameworkCoreTestModule, *******************.EntityFrameworkCore.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. See the inner exception for details. ---- Microsoft.Data.Sqlite.SqliteException : SQLite Error 1: 'table "AbpUsers" already exists'.

  Stack Trace: 
    AbpApplicationBase.ConfigureServices()
    AbpApplicationFactory.Create[TStartupModule](IServiceCollection services, Action`1 optionsAction)
    AbpIntegratedTest`1.ctor()
    ProfileManagementTestBase`1.ctor()
    *******************ApplicationTestBase.ctor()
    *******************.ctor() line 15
    ----- Inner Stack Trace -----
    SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
    SqliteCommand.PrepareAndEnumerateStatements(Stopwatch timer)+MoveNext()
    SqliteCommand.GetStatements(Stopwatch timer)+MoveNext()
    SqliteDataReader.NextResult()
    SqliteCommand.ExecuteReader(CommandBehavior behavior)
    SqliteCommand.ExecuteNonQuery()
    RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
    MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
    MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
    ProfileManagementEntityFrameworkCoreTestModule.CreateDatabaseAndGetConnection() line 68
    ProfileManagementEntityFrameworkCoreTestModule.ConfigureInMemorySqlite(IServiceCollection services) line 37
    ProfileManagementEntityFrameworkCoreTestModule.ConfigureServices(ServiceConfigurationContext context) line 31
    AbpApplicationBase.ConfigureServices()
  • **ABP Framework version: v4.3.1 UI type: Angular DB provider: EF Core Tiered (MVC) or Identity Server Separated (Angular): yes Exception message and stack trace: Steps to reproduce the issue:"

7 Answer(s)
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi @ChetanKumbhar, can you share your ProfileManagementEntityFrameworkCoreTestModule class?

  • User Avatar
    0
    ChetanKumbhar created

    hi @EngincanV can we have online session for this?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi ChetanKumbhar

    Can you share the code of ProfileManagementEntityFrameworkCoreTestModule?

    This error means that you did multiple migrations in the unit test.

  • User Avatar
    0
    ChetanKumbhar created
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Data.Sqlite;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Infrastructure;
    using Microsoft.EntityFrameworkCore.Storage;
    using Microsoft.Extensions.DependencyInjection;
    using Volo.Abp;
    using Volo.Abp.EntityFrameworkCore;
    using Volo.Abp.EntityFrameworkCore.Sqlite;
    using Volo.Abp.Identity.EntityFrameworkCore;
    using Volo.Abp.LanguageManagement.EntityFrameworkCore;
    using Volo.Abp.Modularity;
    using Volo.Abp.PermissionManagement.EntityFrameworkCore;
    using Volo.Abp.SettingManagement.EntityFrameworkCore;
    using Volo.Abp.Uow;
    using Volo.Saas.EntityFrameworkCore;
    
    namespace *************.EntityFrameworkCore
    {
        [DependsOn(
            typeof(*************TestBaseModule),
            typeof(*************EntityFrameworkCoreModule),
            typeof(AbpEntityFrameworkCoreSqliteModule)
            )]
        public class *************EntityFrameworkCoreTestModule : AbpModule
        {
            private SqliteConnection _sqliteConnection;
            public override void ConfigureServices(ServiceConfigurationContext context)
            {
    
                ConfigureInMemorySqlite(context.Services);
                context.Services.AddSingleton<IWebHostEnvironment>(new WebHostEnvironmentMockEntity());
            }
    
            private void ConfigureInMemorySqlite(IServiceCollection services)
            {
                _sqliteConnection = CreateDatabaseAndGetConnection();
    
                Configure<AbpUnitOfWorkDefaultOptions>(options =>
                {
                    options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled;
                });
    
                services.Configure<AbpDbContextOptions>(options =>
                {
                    options.Configure(context =>
                    {
                        context.DbContextOptions.UseSqlite(_sqliteConnection);
    
                    });
                });
            }
    
            public override void OnApplicationShutdown(ApplicationShutdownContext context)
            {
                _sqliteConnection.Dispose();
            }
    
            private static SqliteConnection CreateDatabaseAndGetConnection()
            {
                var connection = new SqliteConnection("Data Source=:memory:");
                connection.Open();
    
                new *************DbContext(
                    new DbContextOptionsBuilder<*************DbContext>().UseSqlite(connection).Options
                    ).GetService<IRelationalDatabaseCreator>().CreateTables();
    
                new IdentityDbContext(
                    new DbContextOptionsBuilder<IdentityDbContext>().UseSqlite(connection).Options
                    ).GetService<IRelationalDatabaseCreator>().CreateTables();
    
                new PermissionManagementDbContext(
                   new DbContextOptionsBuilder<PermissionManagementDbContext>().UseSqlite(connection).Options
                   ).GetService<IRelationalDatabaseCreator>().CreateTables();
    
                new SettingManagementDbContext(
                    new DbContextOptionsBuilder<SettingManagementDbContext>().UseSqlite(connection).Options
                    ).GetService<IRelationalDatabaseCreator>().CreateTables();
    
                new LanguageManagementDbContext(
                   new DbContextOptionsBuilder<LanguageManagementDbContext>().UseSqlite(connection).Options
                   ).GetService<IRelationalDatabaseCreator>().CreateTables();
    
                new SaasDbContext(
                   new DbContextOptionsBuilder<SaasDbContext>().UseSqlite(connection).Options
                   ).GetService<IRelationalDatabaseCreator>().CreateTables();
    
                return connection;
            }
        }
    }
    
    
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer
    using Microsoft.AspNetCore.Hosting; 
    using Microsoft.Data.Sqlite; 
    using Microsoft.EntityFrameworkCore; 
    using Microsoft.EntityFrameworkCore.Infrastructure; 
    using Microsoft.EntityFrameworkCore.Storage; 
    using Microsoft.Extensions.DependencyInjection; 
    using Volo.Abp; 
    using Volo.Abp.EntityFrameworkCore; 
    using Volo.Abp.EntityFrameworkCore.Sqlite; 
    using Volo.Abp.Identity.EntityFrameworkCore; 
    using Volo.Abp.LanguageManagement.EntityFrameworkCore; 
    using Volo.Abp.Modularity; 
    using Volo.Abp.PermissionManagement.EntityFrameworkCore; 
    using Volo.Abp.SettingManagement.EntityFrameworkCore; 
    using Volo.Abp.Uow; 
    using Volo.Saas.EntityFrameworkCore; 
     
    namespace *************.EntityFrameworkCore 
    { 
        [DependsOn( 
            typeof(*************TestBaseModule), 
            typeof(*************EntityFrameworkCoreModule), 
            typeof(AbpEntityFrameworkCoreSqliteModule) 
            )] 
        public class *************EntityFrameworkCoreTestModule : AbpModule 
        { 
            private SqliteConnection _sqliteConnection; 
            public override void ConfigureServices(ServiceConfigurationContext context) 
            { 
     
                ConfigureInMemorySqlite(context.Services); 
                context.Services.AddSingleton<IWebHostEnvironment>(new WebHostEnvironmentMockEntity()); 
            } 
     
            private void ConfigureInMemorySqlite(IServiceCollection services) 
            { 
                _sqliteConnection = CreateDatabaseAndGetConnection(); 
     
                Configure<AbpUnitOfWorkDefaultOptions>(options => 
                { 
                    options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; 
                }); 
     
                services.Configure<AbpDbContextOptions>(options => 
                { 
                    options.Configure(context => 
                    { 
                        context.DbContextOptions.UseSqlite(_sqliteConnection); 
     
                    }); 
                }); 
            } 
     
            public override void OnApplicationShutdown(ApplicationShutdownContext context) 
            { 
                _sqliteConnection.Dispose(); 
            } 
     
            private static SqliteConnection CreateDatabaseAndGetConnection() 
            { 
                var connection = new SqliteConnection("Data Source=:memory:"); 
                connection.Open(); 
     
                new *************DbContext( 
                    new DbContextOptionsBuilder<*************DbContext>().UseSqlite(connection).Options 
                    ).GetService<IRelationalDatabaseCreator>().CreateTables(); 
     
                new IdentityDbContext( 
                    new DbContextOptionsBuilder<IdentityDbContext>().UseSqlite(connection).Options 
                    ).GetService<IRelationalDatabaseCreator>().CreateTables(); 
     
                new PermissionManagementDbContext( 
                   new DbContextOptionsBuilder<PermissionManagementDbContext>().UseSqlite(connection).Options 
                   ).GetService<IRelationalDatabaseCreator>().CreateTables(); 
     
                new SettingManagementDbContext( 
                    new DbContextOptionsBuilder<SettingManagementDbContext>().UseSqlite(connection).Options 
                    ).GetService<IRelationalDatabaseCreator>().CreateTables(); 
     
                new LanguageManagementDbContext( 
                   new DbContextOptionsBuilder<LanguageManagementDbContext>().UseSqlite(connection).Options 
                   ).GetService<IRelationalDatabaseCreator>().CreateTables(); 
     
                new SaasDbContext( 
                   new DbContextOptionsBuilder<SaasDbContext>().UseSqlite(connection).Options 
                   ).GetService<IRelationalDatabaseCreator>().CreateTables(); 
     
                return connection; 
            } 
        } 
    } 
     
    

    You don't need to specify dependent module contexts in here. You only need to define your own db context (*************DbContext) here. So please update your CreateDatabaseAndGetConnection method as below.

    private static SqliteConnection CreateDatabaseAndGetConnection() 
    { 
        var connection = new SqliteConnection("Data Source=:memory:");
       connection.Open();
    
       var options = new DbContextOptionsBuilder<*************DbContext>()
        .UseSqlite(connection)
        .Options;
    
        using (var context = new *************DbContext(options))
        {
            context.GetService<IRelationalDatabaseCreator>().CreateTables();
        }
    
        return connection;
    }
    
  • User Avatar
    0
    ChetanKumbhar created

    If i removed new IdentityDbContext( new DbContextOptionsBuilder<IdentityDbContext>().UseSqlite(connection).Options ).GetService<IRelationalDatabaseCreator>().CreateTables();

            new PermissionManagementDbContext( 
               new DbContextOptionsBuilder&lt;PermissionManagementDbContext&gt;().UseSqlite(connection).Options 
               ).GetService&lt;IRelationalDatabaseCreator&gt;().CreateTables(); 
    
            new SettingManagementDbContext( 
                new DbContextOptionsBuilder&lt;SettingManagementDbContext&gt;().UseSqlite(connection).Options 
                ).GetService&lt;IRelationalDatabaseCreator&gt;().CreateTables(); 
    
            new LanguageManagementDbContext( 
               new DbContextOptionsBuilder&lt;LanguageManagementDbContext&gt;().UseSqlite(connection).Options 
               ).GetService&lt;IRelationalDatabaseCreator&gt;().CreateTables(); 
    
            new SaasDbContext( 
               new DbContextOptionsBuilder&lt;SaasDbContext&gt;().UseSqlite(connection).Options 
               ).GetService&lt;IRelationalDatabaseCreator&gt;().CreateTables(); 
               
    

    Then i am getting following error Message: Volo.Abp.AbpInitializationException : An error occurred during the initialize Volo.Abp.Modularity.OnApplicationInitializationModuleLifecycleContributor phase of the module **************TestBaseModule, **********.TestBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: SQLite Error 1: 'no such table: AbpSettings'.. See the inner exception for details. ---- Microsoft.Data.Sqlite.SqliteException : SQLite Error 1: 'no such table: AbpSettings'.

    Stack Trace: ModuleManager.InitializeModules(ApplicationInitializationContext context) AbpApplicationBase.InitializeModules() AbpApplicationWithExternalServiceProvider.Initialize(IServiceProvider serviceProvider) AbpIntegratedTest1.ctor() **************TestBase1.ctor() ProfileManagementApplicationTestBase.ctor() ARNotificationApplicationServicesTests.ctor() line 29 ----- Inner Stack Trace ----- SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db) SqliteCommand.PrepareAndEnumerateStatements(Stopwatch timer)+MoveNext() <44 more frames...> --- End of stack trace from previous location --- Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) --- End of stack trace from previous location --- TaskExtensions.WaitAndUnwrapException(Task task) AsyncContext.Run(Func1 action) AsyncHelper.RunSync(Func1 action) **************TestBaseModule.SeedTestData(ApplicationInitializationContext context) line 31 **************TestBaseModule.OnApplicationInitialization(ApplicationInitializationContext context) line 26 OnApplicationInitializationModuleLifecycleContributor.Initialize(ApplicationInitializationContext context, IAbpModule module) ModuleManager.InitializeModules(ApplicationInitializationContext context)

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Can you check your YourEntityFrameworkCoreModule depend on AbpSettingManagementEntityFrameworkCoreModule?

    //other dependent modules
    [DependsOn(typeof(AbpSettingManagementEntityFrameworkCoreModule))] //check this line exists or not
    public class YourEntityFrameworkCoreModule : AbpModule
    {
        //...
    }
    
    [ReplaceDbContext(typeof(IIdentityProDbContext))]
    [ReplaceDbContext(typeof(ISaasDbContext))]
    [ConnectionStringName("Default")]
    public class YourProjectDbContext : 
        AbpDbContext<YourProjectDbContext>, 
        IIdentityProDbContext,
        ISaasDbContext
    {
        //...
        
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.ConfigureSettingManagement(); //check this line exists or not
            
            //other configurations (e.g. builder.ConfigureAuditLogging())
        }
    }
    
Made with ❤️ on ABP v9.1.0-preview. Updated on November 18, 2024, 05:54