private static void ConfigureIoc(IServiceCollection services) { services.AddScoped <IRepository <Book>, BookRepository>(); services.AddScoped <IRepository <Lender>, LenderRepository>(); services.AddScoped <ILenderRepository, LenderRepository>(); services.AddScoped <IRepository <LendingRecord>, LendingRecordRepository>(); services.AddScoped <ILendingRecordRepository, LendingRecordRepository>(); services.AddScoped <IRepository <Location>, LocationRepository>(); services.AddScoped <ILocationRepository, LocationRepository>(); services.AddScoped <ICrudInteractor <Book>, BookInteractor>(); services.AddScoped <IBookInteractor, BookInteractor>(); services.AddScoped <ICrudInteractor <Lender>, CrudInteractor <Lender> >(); services.AddScoped <ICrudInteractor <Location>, CrudInteractor <Location> >(); services.AddScoped <ICrudInteractor <LendingRecord>, CrudInteractor <LendingRecord> >(); services.AddScoped <ReportInteractor>(); const string sqlConnectionString = "Server=.;Database=vault13;Trusted_Connection=True;"; services.AddDbContext <DataContext>(options => options.UseSqlServer(sqlConnectionString, builder => { builder.MigrationsAssembly("Vault.DataBase"); })); DatabaseSeed.Seed(services.BuildServiceProvider().GetService <DataContext>()); }
/// <summary> /// Populate databases with fictitious data /// </summary> /// <param name="app"></param> public static void DatabaseSeed(this IApplicationBuilder app) { using (var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope()) { var databaseSeed = new DatabaseSeed(serviceScope.ServiceProvider); databaseSeed.Seed(); } }
private static void SeedDatabase(IApplicationBuilder app) { var context = app.ApplicationServices.GetService <PeopleScreeningContext>(); context.Database.EnsureCreated(); var dbSeed = new DatabaseSeed(context); dbSeed.Seed(); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity <User>() .HasDiscriminator(u => u.Role) .HasValue <Admin>(Role.Admin) .HasValue <Employee>(Role.Employee) .HasValue <RegularUser>(Role.RegularUser); DatabaseSeed.Seed(modelBuilder); base.OnModelCreating(modelBuilder); }
static void Main(string[] args) { var connectionString = args[0]; var builder = new DbContextOptionsBuilder().UseNpgsql(connectionString); var ctx = new EfDbContext(builder.Options); ctx.Database.Migrate(); DatabaseSeed.Seed(ctx); }
public void Configure(IApplicationBuilder app) { app.UseFileServer(); app.UseDeveloperExceptionPage(); app.UseMvc(routes => { routes.MapRoute("Default", "{controller=Home}/{action=Index}"); }); var seeder = new DatabaseSeed(new MoviesData(Configuration)); seeder.Seed(); }
public static void Main(string[] args) { var host = CreateWebHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var context = services.GetService <AppDbContext>(); DatabaseSeed.Seed(context); } host.Run(); }
//U can change this to default later public static void Main(string[] args) { var host = BuildWebHost(args); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var context = services.GetService <AppDbContext>(); var passwordHasher = services.GetService <IPasswordHasher>(); DatabaseSeed.Seed(context, passwordHasher); } host.Run(); }
protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureServices(services => { // Create a new service provider var internalServiceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider(); // Add a database context (MoisContext) using an in-memory database for testing. services.AddDbContext <MoisContext>(options => { options.UseInMemoryDatabase("InMemoryDbForTesting"); options.UseInternalServiceProvider(internalServiceProvider); }); services.AddScoped <IPasswordHasher, PasswordHasher>(); //We have to add the appsettings.json file in the test project and make it copy to output directory to load configs from the file var projectDir = Directory.GetCurrentDirectory(); var configPath = Path.Combine(projectDir, "appsettings.json"); builder.ConfigureAppConfiguration((context, conf) => { conf.AddJsonFile(configPath); }); //Build the service provider var serviceProvider = services.BuildServiceProvider(); // Create a scope to obtain a reference to the database context using (var scope = serviceProvider.CreateScope()) { var scopedServices = scope.ServiceProvider; var db = scopedServices.GetRequiredService <MoisContext>(); var logger = scopedServices.GetRequiredService <ILogger <CustomWebApplicationFactory <TStartup> > >(); //var x = scopedServices.GetService<ITokenVerificationService>(); var passwordHasher = scopedServices.GetRequiredService <IPasswordHasher>(); db.Database.EnsureCreated(); try { //Seed data DatabaseSeed.Seed(db, passwordHasher); } catch (Exception ex) { logger.LogError(ex, $"An error occurred during seeding data. Error: {ex.Message}"); } } }); }
public static void Main(string[] args) { var host = BuildWebHost(args); using (var scope = host.Services.CreateScope()) using (var context = scope.ServiceProvider.GetService <AppDbContext>()) { //services, paswordhasher ,databaseSeedSonradan eklendi sorun çýkarsa silerek denenecek var services = scope.ServiceProvider; context.Database.EnsureCreated(); // var passwordHasher = services.GetService <IPasswordHasher>(); // DatabaseSeed.Seed(context, passwordHasher); } host.Run(); }
public static void Main(string[] args) { /* * Performance: * By default, the file sink will flush each event written through it to disk. * To improve write performance, specifying buffered: true will permit the underlying stream to buffer writes. * The Serilog.Sinks.Async package can be used to wrap the file sink and perform all disk access on a background worker thread. */ Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(Configuration) .Enrich.FromLogContext() .CreateLogger(); //Serilog.Debugging.SelfLog.Enable(msg => //{ // Debug.Print(msg); // Debugger.Break(); //}); try { Log.Information("Application started."); var host = CreateWebHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var context = services.GetService <MoisContext>(); var passwordHasher = services.GetService <IPasswordHasher>(); DatabaseSeed.Seed(context, passwordHasher); } host.Run(); } catch (Exception ex) { Log.Fatal(ex, "Host terminated unexpectedly"); return; } finally { Log.CloseAndFlush(); } }
public static void Main(string[] args) { //this is normal //CreateHostBuilder(args).Build().Run(); //this is only because we are using the in-memory database provider var host = BuildWebHost(args); using (var scope = host.Services.CreateScope()) using (var context = scope.ServiceProvider.GetService <AppDbContext>()) { context.Database.EnsureCreated(); var services = scope.ServiceProvider; var passwordHasher = services.GetService <IPasswordHasher>(); DatabaseSeed.Seed(context, passwordHasher); } host.Run(); }
public static void Main(string[] args) { var host = CreateWebHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetService <ApplicationDatabaseContext>(); var passwordHasher = services.GetService <IPasswordHasher>(); //apply all migrations context.Database.Migrate(); //seed data DatabaseSeed.Seed(context, passwordHasher); } catch (Exception e) { throw e; } } host.Run(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, DatabaseSeed seed) { using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope()) { var context = serviceScope.ServiceProvider.GetRequiredService <MoviesData>(); context.Database.Migrate(); } if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions { HotModuleReplacement = true }); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); routes.MapSpaFallbackRoute( name: "spa-fallback", defaults: new { controller = "Home", action = "Index" }); }); seed.Seed(Configuration).Wait(); }
private void EnsureDatabaseIsCreated(MeetupAppDbContext dbContext) { dbContext.Database.Migrate(); DatabaseSeed.Seed(dbContext); }