コード例 #1
0
        public static async Task Main(string[] args)
        {
            var host  = CreateHostBuilder(args).Build();
            var scope = host.Services.CreateScope();

            using (scope)
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var context     = services.GetRequiredService <StoreContext>();
                    var userManager = services.GetRequiredService <UserManager <User> >();
                    await context.Database.MigrateAsync();

                    await UserSeed.SeedUserAsync(userManager, context);

                    await CategorySeed.SeedCategoryAsync(context);

                    await SubCategorySeed.SeedSubCategory(context);

                    await ProductSeed.SeedProductAsync(context, userManager);

                    await ProductImagesSeed.SeedProductImages(context);

                    await ProductLinkSubCategory.SeedProductLinkSubCategoriesAsync(context);

                    await VIPAdsSeed.SeedVIPAds(context);
                }
                catch (Exception e)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(e, "error happen in migration");
                }
            }
            host.Run();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: TBag/MS-Azure-LOB-ASE
        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var storageService = services.GetRequiredService <IStorageService>();
                    ProductImagesSeed.SeedAsync(storageService, "images", "products", loggerFactory).Wait();

                    var catalogContext = services.GetRequiredService <CatalogContext>();
                    catalogContext.Database.EnsureCreated();
                    CatalogContextSeed.SeedAsync(catalogContext, loggerFactory).Wait();

                    var salesContext = services.GetRequiredService <SalesContext>();
                    salesContext.Database.EnsureCreated();
                    SalesContextSeed.SeedAsync(catalogContext, salesContext, loggerFactory).Wait();

                    var identityContext = services.GetRequiredService <AppIdentityDbContext>();
                    identityContext.Database.EnsureCreated();

                    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();
        }