コード例 #1
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 <StoreContext>();
                    await context.Database.MigrateAsync();

                    await StoreContextSeed.SeedAsync(context, loggerFactory);

                    var userManager     = services.GetRequiredService <UserManager <AppUser> >();
                    var identityContext = services.GetRequiredService <AppIdentityDbContext>();

                    await identityContext.Database.MigrateAsync();

                    await AppIdentityContextSeed.SeedAsync(userManager);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex.Message);
                }
            }

            host.Run();
        }
コード例 #2
0
        /// <summary>
        /// Extension method for ApplicationBuilder to seed user data.
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static async Task SeedData(this IApplicationBuilder builder)
        {
            IServiceProvider provider = builder.ApplicationServices;

            using (var scope = provider.GetService <IServiceScopeFactory>().CreateScope())
            {
                var userManager = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                var roleManager = scope.ServiceProvider.GetRequiredService <RoleManager <IdentityRole> >();

                string userId = await AppIdentityContextSeed.SeedAsync(userManager, roleManager);

                var     webContext = scope.ServiceProvider.GetRequiredService <WebContext>();
                Project project    = await WebContextSeed.SeedAsync(webContext, userId);
            }
        }
コード例 #3
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var catalogContext = scope.ServiceProvider.GetRequiredService <AppDbContext>();

                var userManager = scope.ServiceProvider.GetRequiredService <UserManager <IdentityUser> >();
                var roleManager = scope.ServiceProvider.GetRequiredService <RoleManager <IdentityRole> >();

                AppIdentityContextSeed.SeedASync(userManager, roleManager).Wait();

                AppDbContextSeed.Seed(catalogContext);
            }

            host.Run();
        }
コード例 #4
0
        public static async Task Main(string[] args)
        {
            // While Starting the Application if the database is not there.
            // Any Pending migration not carried will be done with below configuration.

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

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

                try
                {
                    // while program gets started the below code will create the
                    // database Skinet if not there and do the migration if not there
                    var context = services.GetRequiredService <StoreContext>();
                    await context.Database.MigrateAsync();

                    await StoreContextSeed.SeedData(context, loggerFactory);

                    // while program gets started the below code will create the
                    // database Identity if not available along with migration

                    var userManager     = services.GetRequiredService <UserManager <AppUser> >();
                    var identityContext = services.GetRequiredService <AppIdentityDBContext>();
                    await identityContext.Database.MigrateAsync();

                    await AppIdentityContextSeed.SeedUserAsync(userManager);
                }

                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "Failed to migrate");
                }
            }
            host.Run();
        }