using FluentMigrator; using FluentMigrator.Runner; using FluentMigrator.Runner.Conventions; using FluentMigrator.Runner.Initialization; using FluentMigrator.Runner.Processors; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Nop.Core.Configuration; using Nop.Core.Infrastructure; using Nop.Data.Migrations; namespace Nop.Data; /// /// Represents object for the configuring DB context on application startup /// public partial class NopDbStartup : INopStartup { /// /// Add and configure any of the middleware /// /// Collection of service descriptors /// Configuration of the application public void ConfigureServices(IServiceCollection services, IConfiguration configuration) { var typeFinder = Singleton.Instance; var mAssemblies = typeFinder.FindClassesOfType() .Select(t => t.Assembly) .Where(assembly => !assembly.FullName.Contains("FluentMigrator.Runner")) .Distinct() .ToArray(); services // add common FluentMigrator services .AddFluentMigratorCore() .AddScoped() // set accessor for the connection string .AddScoped(x => DataSettingsManager.LoadSettings()) .AddSingleton() .AddSingleton() .ConfigureRunner(rb => rb.WithVersionTable(new MigrationVersionInfo()).AddSqlServer().AddMySql5().AddPostgres() // define the assembly containing the migrations .ScanIn(mAssemblies).For.Migrations()); services.AddTransient(p => new Lazy(p.GetRequiredService())); //data layer services.AddTransient(); services.AddTransient(serviceProvider => serviceProvider.GetRequiredService().DataProvider); //repositories services.AddScoped(typeof(IRepository<>), typeof(EntityRepository<>)); if (!DataSettingsManager.IsDatabaseInstalled()) return; using var scope = services.BuildServiceProvider().CreateScope(); var runner = scope.ServiceProvider.GetRequiredService(); foreach (var assembly in mAssemblies) runner.ApplyUpSchemaMigrations(assembly); } /// /// Configure the using of added middleware /// /// Builder for configuring an application's request pipeline public void Configure(IApplicationBuilder application) { var config = Singleton.Instance.Get(); LinqToDB.Common.Configuration.Linq.DisableQueryCache = config.LinqDisableQueryCache; } /// /// Gets order of this startup configuration implementation /// public int Order => 10; }