Ejemplo n.º 1
0
        /// <summary>
        /// Applies the pending database migrations.
        /// </summary>
        /// <returns>The pending database migrations.</returns>
        /// <param name="webHost">Web host.</param>
        /// <typeparam name="T">DbContext Type</typeparam>
        public static IWebHost ApplyPendingDatabaseMigrations <T>(this IWebHost webHost, ILogger logger) where T : DbContext
        {
            try
            {
                var serviceScopeFactory = webHost.GetServiceScopeFactory();

                using (var scope = serviceScopeFactory?.CreateScope())
                {
                    var dbContext = scope.GetService <T>();

                    dbContext?.Database.Migrate();
                }
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, ex.Message);
                throw;
            }

            return(webHost);
        }