// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); // Database support services.AddScoped <DbConnection, MySqlConnection>(_ => { var connectionString = Configuration.GetConnectionString("LegacyDbContext"); if (string.IsNullOrEmpty(connectionString)) { throw new Exception("Empty [LegacyDbContext] connection string!"); } return(new MySqlConnection(connectionString)); }); services.AddDbContext <LegacyDbContext>((serviceProvider, optionsBuilder) => { var connection = serviceProvider.GetRequiredService <DbConnection>(); optionsBuilder.UseMySql(connection); }); services.AddScoped <UnitOfWorkProperty, AppUnitOfWork>(serviceProvider => { DbTransaction transaction = null; var connection = serviceProvider.GetRequiredService <DbConnection>(); var uow = new AppUnitOfWork(); uow.Property <DbTransaction>(() => { if (connection.State != ConnectionState.Open) { connection.Open(); } if (transaction == null) { transaction = connection.BeginTransaction(); } return(transaction); }); uow.Property <LegacyDbContext>(() => { var context = serviceProvider.GetRequiredService <LegacyDbContext>(); if (context.Database.CurrentTransaction == null) { context.Database.UseTransaction(uow.Property <DbTransaction>()); } return(context); }); return(uow); }); services.AddScoped(typeof(UnitOfWorkProperty <>)); services.AddScoped <FornecedorRepository>(); services.AddScoped <FornecedorRecursoRepository>(); services.AddScoped <FornecedorService>(); }