public AppDataContext CreateDbContext(string[] args) { var dbConnectionInfo = GetDbConnectionInfo(); if (String.IsNullOrWhiteSpace(dbConnectionInfo.connStr)) { throw new InvalidOperationException("Cannot read connection string from coniguration"); } Console.WriteLine($"Using db provider: {dbConnectionInfo.dbProvidername}"); Console.WriteLine($"Using connection string: {dbConnectionInfo.connStr}"); var builder = new DbContextOptionsBuilder <AppDataContext>(); builder.ConfigureDbContext(dbConnectionInfo.dbProvidername, dbConnectionInfo.connStr); return(new AppDataContext(builder.Options)); }
public async Task Execute(string csvPath, int userId, string dbProviderName, string connectionString) { // DB Context var optionsBuilder = new DbContextOptionsBuilder <AppDataContext>(); optionsBuilder.ConfigureDbContext(dbProviderName, connectionString); // Automapper var autoMapperConfig = new MapperConfiguration(cfg => { cfg.AddProfile <DtoProfile>(); }); var mapper = autoMapperConfig.CreateMapper(); // Start execution using (var dbContext = new AppDataContext(optionsBuilder.Options)) { var companyDataService = new CompanyDataService(dbContext, mapper); var contactDataService = new ContactDataService(dbContext, mapper); var dataImportService = new DataImportService(companyDataService, contactDataService); Console.WriteLine("Importing data..."); await ImportContactsAsync(dataImportService, csvPath, userId); Console.WriteLine("Done"); } }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.ConfigureDbContext(serviceProvider); base.OnConfiguring(optionsBuilder); }