예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="appSettings"></param>
        /// <returns></returns>
        public bool CheckForUpdateAndCreateMssqlMdf(AppSettings appSettings = null)
        {
            var isCheckForUpdateAndCreateMssqlMdfResult = false;

            appSettings ??= _appSettings;
            try
            {
                var result = (DateTime.Now - appSettings.LastMigrateDateTime).Days;
                Log4net.Debug($"CheckForUpdateAndCreateMssqlMdf, compare { DateTime.Now } and { appSettings.LastMigrateDateTime } is { result } CheckForUpdateEveryDays is { appSettings.CheckForUpdateEveryDays }");
                if (result >= appSettings.CheckForUpdateEveryDays)
                {
                    DatabaseMssqlMdf.GetInstance(Database.GetDbConnection().ConnectionString).Create();
                    isCheckForUpdateAndCreateMssqlMdfResult = true;
                }
            }
            catch (Exception e)
            {
                Log4net.Error(string.Format("\n{0}\n{1}\n{2}\n{3}\n", e.GetType(), e.InnerException?.GetType(), e.Message, e.StackTrace), e);
            }
            finally
            {
                if (null != appSettings)
                {
                    appSettings.LastMigrateDateTime = DateTime.Now;
                    Repository.AppSettingsRepository.GetInstance().Save(appSettings);
                }
            }
            return(isCheckForUpdateAndCreateMssqlMdfResult);
        }
예제 #2
0
        public static async Task RunMigrationAsync <TDbContext>(TDbContext context) where TDbContext : DbContext
        {
            try
            {
                try
                {
                    await DatabaseMssqlMdf.GetInstance(context?.Database?.GetDbConnection()?.ConnectionString)
                    .CreateAsync();
                }
                catch (Exception e)
                {
                    Log4Net.Warn(e);
                    if (null != e.InnerException)
                    {
                        Log4Net.Warn(e.InnerException);
                    }
                }

                var isPendingMigrations =
                    (await(context.Database ?? throw new InvalidOperationException()).GetPendingMigrationsAsync())
                    .Any();
                if (isPendingMigrations)
                {
                    try
                    {
                        await context.Database.MigrateAsync();
                    }
                    catch (Exception e)
                    {
                        Log4Net.Warn(e);
                        if (null != e.InnerException)
                        {
                            Log4Net.Warn(e.InnerException);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log4Net.Error(e);
                if (null != e.InnerException)
                {
                    Log4Net.Error(e.InnerException);
                }
            }
        }