// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, HwtaDbContext context) { context.Database.Migrate(); var dbSeed = new DataBaseSeed(context); dbSeed.Seed(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "HWTA_Web v1"); }); app.UseRouting(); app.UseCors(builder => builder.SetIsOriginAllowed(_ => true) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); }); }
public void SeedConfiguration() { ApplicationUserSeed.SeedApplicationUsers(Context); DataBaseSeed.DataBaseSeedMethod(Context); }
public IActionResult Overview() { var db = new DataBaseSeed(); var model = new UserDatabase { Users = db.DB }; return(View(model)); }
protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureServices(services => { services.AddEntityFrameworkInMemoryDatabase(); var provider = services .AddEntityFrameworkInMemoryDatabase() .BuildServiceProvider(); services.AddDbContext <SiteContext>(options => { options.UseInMemoryDatabase("InMemoryDbForTesting"); options.UseInternalServiceProvider(provider); }); var sp = services.BuildServiceProvider(); using (var scope = sp.CreateScope()) { var scopedServices = scope.ServiceProvider; var db = scopedServices.GetRequiredService <SiteContext>(); var loggerFactory = scopedServices.GetRequiredService <ILoggerFactory>(); var logger = scopedServices .GetRequiredService <ILogger <CustomWebApplicationFactory> >(); db.Database.EnsureDeleted(); db.Database.EnsureCreated(); try { DataBaseSeed.SeedShopAsync(db).Wait(); var userManager = scopedServices.GetRequiredService <UserManager <IdentityUser> >(); var roleManager = scopedServices.GetRequiredService <RoleManager <IdentityRole> >(); DataBaseSeed.SeedIdentityAsync(userManager, roleManager).Wait(); } catch (Exception ex) { logger.LogError(ex, $"An error occurred seeding the " + "database with test messages. Error: {ex.Message}"); } } }); }
public static async Task Main(string[] args) { var host = CreateHostBuilder(args).Build(); Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .Enrich.FromLogContext() .WriteTo.File("log.txt", rollingInterval: RollingInterval.Hour) .CreateLogger(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var loggerFactory = services.GetRequiredService <ILoggerFactory>(); try { var siteContext = services.GetRequiredService <SiteContext>(); if (siteContext.Database.EnsureCreated()) { await DataBaseSeed.SeedShopAsync(siteContext); } if (!siteContext.Users.Any()) { var userManager = services.GetRequiredService <UserManager <IdentityUser> >(); var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >(); await DataBaseSeed.SeedIdentityAsync(userManager, roleManager); } } catch (Exception ex) { Log.Error(ex, "Cant create users"); } } host.Run(); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { DataBaseSeed.Seed(modelBuilder); base.OnModelCreating(modelBuilder); }