using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.DependencyInjection; using MyCompanyName.MyProjectName.EntityFrameworkCore; using Volo.Abp; using Volo.Abp.Autofac; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.Modularity; namespace MyCompanyName.MyProjectName { [DependsOn( typeof(MyProjectNameApplicationModule), typeof(MyProjectNameEntityFrameworkCoreModule), typeof(AbpAutofacModule), typeof(AbpTestBaseModule) )] public class MyProjectNameApplicationTestModule : AbpModule { private SqliteConnection _sqliteConnection; public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAlwaysAllowAuthorization(); ConfigureInMemorySqlite(context.Services); } private void ConfigureInMemorySqlite(IServiceCollection services) { _sqliteConnection = CreateDatabaseAndGetConnection(); services.Configure(options => { options.Configure(context => { context.DbContextOptions.UseSqlite(_sqliteConnection); }); }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) { SeedTestData(context); } public override void OnApplicationShutdown(ApplicationShutdownContext context) { _sqliteConnection.Dispose(); } private static SqliteConnection CreateDatabaseAndGetConnection() { var connection = new SqliteConnection("Data Source=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; using (var context = new MyProjectNameDbContext(options)) { context.GetService().CreateTables(); } return connection; } private static void SeedTestData(ApplicationInitializationContext context) { using (var scope = context.ServiceProvider.CreateScope()) { scope.ServiceProvider .GetRequiredService() .Build(); } } } }