/// <summary>
        /// Migrate and seed the DbContext using the seed delegate.
        /// </summary>
        /// <typeparam name="TContext">The DbContext type</typeparam>
        /// <param name="host">The host that will execute the migration.</param>
        /// <param name="seeder">The seed delegate.</param>
        /// <returns>A task that represents the asynchronous migration operation.</returns>
        public static Task <IHost> MigrateDbContext <TContext>(this IHost host, AsyncSeedMethod <TContext> seeder)
            where TContext : DbContext
        {
            var retry = Policy.Handle <SqlException>()
                        .WaitAndRetryAsync(new TimeSpan[]
            {
                TimeSpan.FromSeconds(3),
                TimeSpan.FromSeconds(5),
                TimeSpan.FromSeconds(8),
            });

            return(_migrateDbContext(host, retry, seeder));
        }
        /// <summary>
        /// Migrate and seed the DbContext using the seed delegate.
        /// </summary>
        /// <typeparam name="TContext">The DbContext type</typeparam>
        /// <param name="hostTask">The task that can be awaited to get the host.</param>
        /// <param name="seeder">The seed delegate.</param>
        /// <returns>A task that represents the asynchronous migration operation.</returns>
        public static async Task <IHost> MigrateDbContext <TContext>(this Task <ElseExecuteHost> hostTask, AsyncSeedMethod <TContext> seeder)
            where TContext : DbContext
        {
            var host = await hostTask;

            return(await host.MigrateDbContext(seeder));
        }
        private static async Task <IHost> _migrateDbContext <TContext>(this IHost host, IAsyncPolicy retryPolicy, AsyncSeedMethod <TContext> seeder)
            where TContext : DbContext
        {
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                var logger = services.GetRequiredService <ILogger <TContext> >();

                var context = services.GetService <TContext>();

                try
                {
                    logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}");

                    await retryPolicy
                    .ExecuteAsync(async() =>
                    {
                        await context.Database.MigrateAsync().ConfigureAwait(false);
                        await seeder(context, services).ConfigureAwait(false);
                    })
                    .ConfigureAwait(false);


                    logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}");
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}");
                }
            }

            return(host);
        }
Пример #4
0
        /// <summary>
        /// Migrate and seed the DbContext using the seed delegate.
        /// </summary>
        /// <typeparam name="TContext">The DbContext type</typeparam>
        /// <typeparam name="TMigrationsConfiguration">The type of the migrations configuration to use during initialization.</typeparam>
        /// <param name="hostTask">The task that can be awaited to get the host.</param>
        /// <param name="seeder">The seed delegate.</param>
        /// <returns>A task that represents the asynchronous migration operation.</returns>
        public static async Task <IHost> MigrateDbContext <TContext, TMigrationsConfiguration>(this Task <ElseExecuteHost> hostTask, AsyncSeedMethod <TContext> seeder)
            where TContext : DbContext
            where TMigrationsConfiguration : DbMigrationsConfiguration <TContext>, new()
        {
            var host = await hostTask;

            return(await host.MigrateDbContext <TContext, TMigrationsConfiguration>(seeder));
        }