Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using var scope = host.Services.CreateScope();
            var services      = scope.ServiceProvider;
            var loggerFactory = services.GetRequiredService <ILoggerFactory>();

            try
            {
                // Delete and recreate database
                var context = scope.ServiceProvider.GetService <AppDbContext>();
                context.Database.EnsureDeleted();
                context.Database.Migrate();

                // Populate
                AppDbContextSeed.SeedAsync(context, loggerFactory).Wait();
            }
            catch (Exception ex)
            {
                var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >();
                logger.LogError(ex, "An error occurred while migrating the database.");
            }

            host.Run();
        }
Exemplo n.º 2
0
        public async static Task Main(string[] args)
        {
            var host = CreateWebHostBuilder(args)
                       .Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var appdbContext = services.GetRequiredService <AppDbContext>();
                    await AppDbContextSeed.SeedAsync(appdbContext);

                    //testdatabase;
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }

            host.Run();
        }
Exemplo n.º 3
0
        public async static Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var context = services.GetRequiredService <AppDbContext>();
                    context.Database.Migrate();

                    await AppDbContextSeed.SeedAsync(context);
                }
                catch (Exception ex)
                {
                    var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >();

                    logger.LogError(ex, "An error occurred while migrating or seeding the database.");
                }
            }

            host.Run();
        }
Exemplo n.º 4
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();

                try
                {
                    var dbContext   = services.GetRequiredService <AppDbContext>();
                    var userManager = services.GetRequiredService <UserManager <AppUser> >();
                    var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();

                    await AppDbContextSeed.SeedAsync(dbContext, userManager, roleManager, loggerFactory);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "Error with seeding the data base.");
                }
            }

            host.Run();
        }
Exemplo n.º 5
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();

                try
                {
                    var context = services.GetRequiredService <AppDbContext>();
                    await context.Database.MigrateAsync();

                    await AppDbContextSeed.SeedAsync(context, loggerFactory);

                    var userManager     = services.GetRequiredService <UserManager <AppUser> >();
                    var roleManager     = services.GetRequiredService <RoleManager <AppRole> >();
                    var identityContext = services.GetRequiredService <AppIdentityDbContext>();
                    await identityContext.Database.MigrateAsync();

                    await AppIdentityDbContextSeed.SeedUsersAsync(userManager, roleManager);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError("An error occured during migration", ex.Message);
                }
            }

            host.Run();
        }
Exemplo n.º 6
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();

                try
                {
                    var dbContext = services.GetRequiredService <ApplicationDbContext>();
                    await dbContext.Database.MigrateAsync();

                    await AppDbContextSeed.SeedAsync(dbContext, loggerFactory);

                    var userManager     = services.GetRequiredService <UserManager <AppUser> >();
                    var roleManager     = services.GetRequiredService <RoleManager <AppRole> >();
                    var identityContext = services.GetRequiredService <AppIdentityDbContext>();
                    await identityContext.Database.MigrateAsync();

                    await AppIdentityDbContextSeed.SeedUsersAsync(userManager, roleManager);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError($"Could not create or migrate database, see full error: {ex}");
                }
            }

            host.Run();
        }
Exemplo n.º 7
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args)
                       .UseUrls("http://127.0.0.1:5001")
                       .Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var dbContext = services.GetRequiredService <AppDbContext>();
                    AppDbContextSeed.SeedAsync(dbContext, loggerFactory)
                    .Wait();

                    //var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
                    //AppIdentityDbContextSeed.SeedAsync(userManager).Wait();
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }

            host.Run();
        }
Exemplo n.º 8
0
        public async static Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var context     = services.GetRequiredService <AppDbContext>();
                    var userManager = services.GetRequiredService <UserManager <AppUser> >();
                    var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();

                    // Migration starting on dotnet run, no need for update-database in cli
                    context.Database.Migrate();

                    await AppDbContextSeed.SeedAsync(context, userManager, roleManager);
                }
                catch (Exception ex)
                {
                    var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >();

                    logger.LogError(ex, "An error occurred while migrating or seeding the database.");

                    throw;
                }
            }

            await host.RunAsync();
        }
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureServices(services =>
            {
                services.AddEntityFrameworkInMemoryDatabase();

                // Create a new service provider.
                var provider = services
                               .AddEntityFrameworkInMemoryDatabase()
                               .BuildServiceProvider();

                // Add a database context (ApplicationDbContext) using an in-memory
                // database for testing.
                services.AddDbContext <AppDbContext>(options =>
                {
                    options.UseInMemoryDatabase("InMemoryDbForTesting");
                    options.UseInternalServiceProvider(provider);
                });

                //services.AddIdentity<ApplicationUser, IdentityRole>()
                //        .AddDefaultUI(UIFramework.Bootstrap4)
                //        .AddEntityFrameworkStores<AppIdentityDbContext>()
                //        .AddDefaultTokenProviders();

                // Build the service provider.
                var sp = services.BuildServiceProvider();

                // Create a scope to obtain a reference to the database
                // context (ApplicationDbContext).
                using (var scope = sp.CreateScope())
                {
                    var scopedServices = scope.ServiceProvider;
                    var db             = scopedServices.GetRequiredService <AppDbContext>();
                    var loggerFactory  = scopedServices.GetRequiredService <ILoggerFactory>();

                    var logger = scopedServices
                                 .GetRequiredService <ILogger <CustomWebApplicationFactory <TStartup> > >();

                    // Ensure the database is created.
                    db.Database.EnsureCreated();

                    try
                    {
                        // Seed the database with test data.
                        AppDbContextSeed.SeedAsync(db).Wait();                        // seed sample user data
                        //var userManager = scopedServices.GetRequiredService<UserManager<ApplicationUser>>();
                        // AppIdentityDbContextSeed.SeedAsync(userManager).Wait();
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"An error occurred seeding the " +
                                        "database with test messages. Error: {ex.Message}");
                    }
                }
            });
        }
Exemplo n.º 10
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services  = scope.ServiceProvider;
                var dbContext = services.GetRequiredService <AppDbContext>();
                AppDbContextSeed.SeedAsync(dbContext);
            }

            host.Run();
        }
Exemplo n.º 11
0
        protected async Task <EfRepository <T> > GetRepositoryAsync <T>() where T : class, IAggregateRoot
        {
            //var options = CreateInMemoryContextOptions();
            var options      = CreateSqlLiteOptions();
            var mockMediator = new Mock <IMediator>();

            _dbContext = new TestContext(options, mockMediator.Object);

            var logger           = new LoggerFactory().CreateLogger <AppDbContextSeed>();
            var appDbContextSeed = new AppDbContextSeed(_dbContext, logger);
            await appDbContextSeed.SeedAsync(new OfficeSettings().TestDate);

            return(new EfRepository <T>(_dbContext));
        }
Exemplo n.º 12
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var appDbContext = scope.ServiceProvider.GetRequiredService <AppDbContext>();
                var userManager  = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                var roleManager  = scope.ServiceProvider.GetRequiredService <RoleManager <IdentityRole> >();
                await AppDbContextSeed.SeedAsync(appDbContext);

                await AppIdentityDbContextSeed.SeedAsync(userManager, roleManager);
            }
            host.Run();
        }
Exemplo n.º 13
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())//startupda kullanabileceðim bir servis scoppe olusturuyor
            {
                var appDbContext = scope.ServiceProvider.GetRequiredService <AppDbContext>();
                await appDbContext.Database.MigrateAsync();

                var appIdentityDbContext = scope.ServiceProvider.GetRequiredService <AppIdentityDbContext>();
                await appIdentityDbContext.Database.MigrateAsync();

                var userManager = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                var roleManager = scope.ServiceProvider.GetRequiredService <RoleManager <IdentityRole> >();
                await AppDbContextSeed.SeedAsync(appDbContext);

                await AppIdentityDbContextSeed.SeedAsync(userManager, roleManager);
            }
            host.Run();
        }
Exemplo n.º 14
0
        private void Seed()
        {
            lock (_lock)
            {
                if (!_databaseInitialized)
                {
                    using (var context = CreateContext())
                    {
                        context.Database.EnsureDeleted();
                        //context.Database.EnsureCreated();

                        var logger           = new LoggerFactory().CreateLogger <AppDbContextSeed>();
                        var appDbContextSeed = new AppDbContextSeed(context, logger);
                        appDbContextSeed.SeedAsync(new OfficeSettings().TestDate).Wait();
                        context.SaveChanges();
                    }

                    _databaseInitialized = true;
                }
            }
        }
Exemplo n.º 15
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args)
                       .Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var catalogContext = services.GetRequiredService <AppDbContext>();
                    AppDbContextSeed.SeedAsync(catalogContext, loggerFactory).Wait();
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }
            host.Run();
        }
Exemplo n.º 16
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var context = services.GetRequiredService <AppDbContext>();
                    await context.Database.MigrateAsync();

                    await AppDbContextSeed.SeedAsync(context, loggerFactory);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occured during migration");
                }
            }
            host.Run();
        }
Exemplo n.º 17
0
        public async static Task Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            //host.MigrateDbContext<AppIdentityDbContext>((_, __) => { });
            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var userManager = services.GetRequiredService <UserManager <AppUser> >();
                    await AppIdentityDbContextSeed.SeedAsync(userManager);

                    var appDbContext = services.GetRequiredService <AppDbContext>();
                    await AppDbContextSeed.SeedAsync(appDbContext, loggerFactory, userManager);
                } catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }
            host.Run();
        }
Exemplo n.º 18
0
        public async static Task Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                         .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
                         .MinimumLevel.Override("System", LogEventLevel.Warning)
                         .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
                         .Enrich.FromLogContext()
                         // uncomment to write to Azure diagnostics stream
                         //.WriteTo.File(
                         //    @"D:\home\LogFiles\Application\identityserver.txt",
                         //    fileSizeLimitBytes: 1_000_000,
                         //    rollOnFileSizeLimit: true,
                         //    shared: true,
                         //    flushToDiskInterval: TimeSpan.FromSeconds(1))
                         .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code)
                         .CreateLogger();

            try
            {
                Log.Information("Starting host...");

                var host = CreateHostBuilder(args).Build();

                using (var scope = host.Services.CreateScope())
                {
                    var services = scope.ServiceProvider;

                    try
                    {
                        var context = services.GetRequiredService <AppDbContext>();
                        var idsvPersistedGrantContext = services.GetRequiredService <PersistedGrantDbContext>();
                        var idsvConfigContext         = services.GetRequiredService <ConfigurationDbContext>();

                        if (context.Database.IsSqlServer())
                        {
                            context.Database.Migrate();
                            idsvPersistedGrantContext.Database.Migrate();
                            idsvConfigContext.Database.Migrate();
                        }

                        var userManager = services.GetRequiredService <UserManager <ApplicationUser> >();

                        await AppDbContextSeed.SeedAsync(context, userManager);
                    }
                    catch (Exception ex)
                    {
                        Log.Fatal(ex, "An error occurred while migrating or seeding the database.");
                    }
                }

                await host.RunAsync();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly.");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }