Ejemplo n.º 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);
         }
         catch(Exception ex)
         {
             var logger = loggerFactory.CreateLogger<Program>();
             logger.LogError(ex, "Error occured during migration");
         }
     }
     host.Run();
 }
Ejemplo n.º 2
0
 public static async Task Main(string[] args)
 {
     var host = CreateHostBuilder(args).Build();
     using (var scope = host.Services.CreateScope())
     {
         NLog.Logger logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
         var services = scope.ServiceProvider;
         var loggerFactory = services.GetRequiredService<ILoggerFactory>().CreateLogger<Program>();
         try
         {
             var context = services.GetRequiredService<StoreContext>();
             await context.Database.MigrateAsync();
             await StoreContextSeed.SeedAsync(context, loggerFactory);
         }
         catch (Exception ex)
         {
             logger.Error(ex, "An error occured during migration");
         }
     }
     host.Run();
 }
Ejemplo n.º 3
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            // Apply migrations and create database if it doesn't exist.
            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();

                    // seed data into database using json files
                    await StoreContextSeed.SeedAsync(context, loggerFactory);
                } catch (Exception e) {
                    var logger = loggerFactory.CreateLogger <Program> ();
                    logger.LogError(e, "An error occured during migration");
                }
            }
            host.Run();
        }
Ejemplo n.º 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();
        }
Ejemplo n.º 5
0
        public static async Task Main(string[] args)
        {
            // var config = new ConfigurationBuilder()
            // .AddJsonFile("appsettings.Development.json").Build();
            var host = CreateHostBuilder(args).Build();

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

            try
            {
                CreateHostBuilder(args).Build().Run();
                var context = services.GetRequiredService <StoreContext>();
                await StoreContextSeed.SeedAsync(context, loggerFactory);
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <Program>();
                logger.LogError(ex, "An error occurred during migration");
            }
        }
Ejemplo n.º 6
0
        public static async Task Main(string[] args)
        {
            // Bu kısım proje çalıştırıldığında eğer data contextte bir değişiklik var ise bunu data base e aktarıyor
            //
            var host = CreateHostBuilder(args).Build();

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

                //olası hataları yakalamak ıcın logger factory servisi çağırılıyor. catch blogunda da olası hata durumunda hata geliştiriciye gösteriliyor.
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    //alttaki kod migration oluşturulacak context'i bulup context değişkenine atıyor.
                    var context = services.GetRequiredService <StoreContext>();

                    //üst tarafta açıklanan context ile data base oluşturmak için bağlantı adresi nokta database nokta migration kullanılıyor tabiki işlemin süresi boyunca program durdurulmaması için async olarak yapılıyor.
                    await context.Database.MigrateAsync();

                    //Burada hazır verileri data base e yolluyoruz.
                    await StoreContextSeed.SeedAsync(context, loggerFactory);


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

                    await AppIdentityDbContextSeed.SeedUserAsync(userManager);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred during migration");
                }
            }
            host.Run();
        }
Ejemplo n.º 7
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                // ILoggerFactory pour le jounal des log
                var loggerFactor = services.GetRequiredService <ILoggerFactory>();
                // crée la base si n'existe pas
                try
                {
                    //ajout des données et création de la base skinet.db
                    // et des tables si besoin
                    var context = services.GetRequiredService <StoreContext>();
                    await context.Database.MigrateAsync();

                    // appel de la méthode de seed
                    await StoreContextSeed.SeedAsync(context, loggerFactor);


                    //ajout des données et création de la base skinet.db
                    // et des tables si besoin
                    var identityContext = services.GetRequiredService <AppIdentityDbContext>();
                    var userManager     = services.GetRequiredService <UserManager <AppUser> >();
                    await identityContext.Database.MigrateAsync();

                    // appel de la methode seed
                    await AppIdentityDbContextSeed.SeedUserAsync(userManager);
                }
                catch (Exception e)
                {
                    var logger = loggerFactor.CreateLogger <Program>();
                    logger.LogError(e, "Erreur creation base");
                }
            }
            host.Run();
        }
Ejemplo n.º 8
0
        public async static Task Main(string[] args)
        {
            //executes a process
            var host = CreateHostBuilder(args).Build();

            //essentially you need to get the services, which is part of the host
            //these are available in the startup class too
            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    //serices provider gets access to all things dependency injection
                    var context = services.GetRequiredService <StoreContext>();
                    await context.Database.MigrateAsync();

                    await StoreContextSeed.SeedAsync(context, loggerFactory);

                    var userManager = services.GetRequiredService <UserManager <AppUser> >();
                    // need reference to identity db context
                    var identityContext = services.GetRequiredService <AppIdentityDbContext>();
                    await identityContext.Database.MigrateAsync();

                    // applies all pending migrations to the database
                    await AppIdentityDbContextSeed.SeedUsersAsync(userManager);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occured during migration");
                }
            }



            host.Run();
        }
Ejemplo n.º 9
0
        public async static Task Main(string[] args)
        {
            //Cria banco de dados caso não exista
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    //Cria contexto "Store" e inclui dados seed
                    var context = services.GetRequiredService <StoreContext>();
                    await context.Database.MigrateAsync();

                    await StoreContextSeed.SeedAsync(context, loggerFactory);

                    //Identity - Passo 9: Recupera referênca "UserManager" para "AppUser"
                    var userManager = services.GetRequiredService <UserManager <AppUser> >();

                    //Identity - Passo 10: Recupera referência para AppIdentityDbContext
                    var identityContext = services.GetRequiredService <AppIdentityDbContext>();

                    //Identity - Passo 11: Tentar criar banco de dados
                    await identityContext.Database.MigrateAsync();

                    //Identity - Passo 12: Inclui dados seed no contexto
                    await AppIdentityDbContextSeed.SeedUsersAsync(userManager);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error ocurred during migration");
                }
            }
            host.Run();
        }
Ejemplo n.º 10
0
        // most important file?  main?  Main runs first builds


        public static async Task Main(string[] args)
        {
            //CreateHostBuilder(args).Build().Run();

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

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    //var userSampleContext = services.GetRequiredService<DataContext>();
                    //await userSampleContext.Database.MigrateAsync();
                    //await Seed.SeedAsync(context, loggerFactory);
                    //await Seed.SeedUsersAsync(userSampleContext, loggerFactory);

                    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 AppIdentityDbContextSeed.SeedUsersAsync(userManager);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occured during migration");
                }
            }

            host.Run();
        }
Ejemplo n.º 11
0
        //called when started the project
        public static async Task Main(string[] args)
        {
            //configure host
            var host = CreateHostBuilder(args).Build();

            //access to data context
            //don't have control to lifetime
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                //log any info
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var context = services.GetRequiredService <StoreContext>();
                    await context.Database.MigrateAsync(); //apply pending migration to db or create db if not exists

                    await StoreContextSeed.SeedAsync(context, loggerFactory);

                    // add user manager
                    var userManager     = services.GetRequiredService <UserManager <AppUser> >();
                    var identityContext = services.GetRequiredService <AppIdentityDbContext>();

                    await identityContext.Database.MigrateAsync();

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

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

            using (var scope = host.Services.CreateScope()) // creating the service scope
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var context = services.GetRequiredService <StoreContext>();
                    await context.Database.MigrateAsync(); // apply the pending migration to the databse / if db do not exist then it will create the database.

                    await StoreContextSeed.SeedAsync(context, loggerFactory);
                }
                catch (Exception e)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError("Error occured during migration" + e.Message);
                }
            }
            host.Run();
        }
Ejemplo n.º 13
0
        public static async Task Main(string[] args)
        {
            //only called during first time, during worker process initialiation.At subsequent requests it wont be called
            var host = CreateHostBuilder(args).Build();// Run the given actions to initialize the host. This can only be called once.

            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);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occured during migration");
                }
            }
            host.Run();
        }
Ejemplo n.º 14
0
        public static async Task Main(string[] args)
        {
            //old method
            //CreateHostBuilder(args).Build().Run();

            //new method so that Migration cab be run automatically
            //i.e when the DB does not exist e.g at deployment
            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 AppIdentityDbContextSeed.SeeduserAsync(userManager);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occured during Migration");
                }
            }

            host.Run();
        }
Ejemplo n.º 15
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); // we can call this method directly because this is a static so we do not need to create an instance from a class
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occured during migration");
                }
            }

            host.Run();
        }