public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); app.UseStatusCodePagesWithRedirects("/Error/{0}"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseSession(); app.UseAuthentication(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=HomePage}/{id?}" ); endpoints.MapControllerRoute( name: null, pattern: "{category}/Page{productPage:int}", defaults: new { controller = "Home", action = "Index" } ); endpoints.MapControllerRoute( name: null, pattern: "Page{productPage:int}", defaults: new { controller = "Home", action = "Index", productPage = 1 } ); endpoints.MapControllerRoute( name: null, pattern: "{category}", defaults: new { controller = "Home", action = "Index", productPage = 1 } ); endpoints.MapControllerRoute( name: null, pattern: "", defaults: new { controller = "Home", action = "Index", productPage = 1 } ); endpoints.MapControllerRoute( name: null, pattern: "{controller}/{action}/{id?}"); }); AppDataSeed.SeedData(app); }
public ProductRepositoryTests(ITestOutputHelper output) { _output = output; var dbOptions = new DbContextOptionsBuilder <AppDataContext>() .UseInMemoryDatabase(databaseName: "AspNetCoreWebApiTest") .Options; _appDataContext = new AppDataContext(dbOptions); AppDataSeed.SeedAsync(_appDataContext, false).Wait(); _productRepository = new ProductRepository(_appDataContext); }
protected override void ConfigureDatabases(IServiceCollection services) { services.AddDbContext <AppDataContext>(c => c.UseInMemoryDatabase("AspNetCoreWebApiTest").UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking), ServiceLifetime.Transient); using (var scope = services.BuildServiceProvider().CreateScope()) { var scopedServices = scope.ServiceProvider; var AspNetCoreWebApiContext = scopedServices.GetRequiredService <AppDataContext>(); AspNetCoreWebApiContext.Database.EnsureCreated(); AppDataSeed.SeedAsync(AspNetCoreWebApiContext, false).Wait(); } }
// Beispielhaft für Azure Hosting private void MigrateAndSeedDatabase(string connectionString) { var optionsBuilder = new DbContextOptionsBuilder <DataContext>(); optionsBuilder.UseSqlServer(this.Configuration.GetConnectionString(connectionString)); using (var db = new DataContext(optionsBuilder.Options)) { db.Database.Migrate(); AppDataSeed.Init(db); } }
public void DoIt() { var connectionString = "Data Source=.\\SQLEXPRESS;Database=Todos;Integrated Security=True;"; var builder = new DbContextOptionsBuilder <DataContext>() .UseSqlServer(connectionString); using (var db = new DataContext(builder.Options)) { db.Database.EnsureDeleted(); db.Database.EnsureCreated(); AppDataSeed.Init(db); } Assert.True(true); }
/// <summary> /// /// </summary> /// <param name="host"></param> private static void SeedData(IHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var logger = services.GetRequiredService <IAppLogger <AppDataSeed> >(); try { var AspNetCoreWebContext = services.GetRequiredService <AppDataContext>(); AppDataSeed.SeedAsync(AspNetCoreWebContext, true).Wait(); } catch (Exception ex) { logger.LogError("An error occurred seeding the DB.", ex); } } }