コード例 #1
0
        public async Task InitDBAsync(string path, Action <string> OnUpdateMessage)
        {
            string content = await File.ReadAllTextAsync(path);

            InitDatas = JsonConvert.DeserializeObject <InitDatas>(content);
            Random random = new Random();

            context.Database.SetCommandTimeout(TimeSpan.FromMinutes(3));

            #region 適用於 Code First ,刪除資料庫與移除資料庫
            string Msg = "";
            Msg = $"適用於 Code First ,刪除資料庫與移除資料庫";
            OnUpdateMessage(Msg);
            Logger.LogInformation($"{Msg}");
            await context.Database.EnsureDeletedAsync();

            Msg = $"刪除資料庫";
            OnUpdateMessage(Msg);
            Logger.LogInformation($"{Msg}");
            await context.Database.EnsureCreatedAsync();

            Msg = $"建立資料庫";
            OnUpdateMessage(Msg);
            try
            {
                await SystemLogHelper.LogAsync(new SystemLogAdapterModel()
                {
                    Message    = Msg,
                    Category   = LogCategories.Initialization,
                    Content    = "",
                    LogLevel   = LogLevels.Information,
                    Updatetime = DateTime.Now,
                    IP         = HttpContextAccessor.GetConnectionIP(),
                });
            }
            catch (Exception)
            {
            }
            Logger.LogInformation($"{Msg}");
            #endregion

            #region 還原預設紀錄
            DateTime currentNow;
            #region AccountPolicy
            currentNow = DateTime.Now;
            Msg        = $"建立 AccountPolicy";
            OnUpdateMessage(Msg);
            await context.AccountPolicy.AddRangeAsync(InitDatas.AccountPolicy);

            await context.SaveChangesWithoutIdentityInsertAsync <AccountPolicy>();

            OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
            #endregion

            #region MenuRole
            currentNow = DateTime.Now;
            Msg        = $"建立 MenuRole";
            OnUpdateMessage(Msg);
            await context.MenuRole.AddRangeAsync(InitDatas.MenuRole);

            await context.SaveChangesWithoutIdentityInsertAsync <MenuRole>();

            OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
            #endregion

            #region MenuData
            currentNow = DateTime.Now;
            Msg        = $"建立 MenuData";
            OnUpdateMessage(Msg);
            await context.MenuData.AddRangeAsync(InitDatas.MenuData);

            await context.SaveChangesWithoutIdentityInsertAsync <MenuData>();

            OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
            #endregion

            #region MyUser
            currentNow = DateTime.Now;
            Msg        = $"建立 MyUser";
            OnUpdateMessage(Msg);
            await context.MyUser.AddRangeAsync(InitDatas.MyUser);

            await context.SaveChangesWithoutIdentityInsertAsync <MyUser>();

            OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
            #endregion

            #region MyUserPasswordHistory
            currentNow = DateTime.Now;
            Msg        = $"建立 MyUserPasswordHistory";
            OnUpdateMessage(Msg);
            await context.MyUserPasswordHistory.AddRangeAsync(InitDatas.MyUserPasswordHistory);

            await context.SaveChangesWithoutIdentityInsertAsync <MyUserPasswordHistory>();

            OnUpdateMessage($"{Msg} ({DateTime.Now - currentNow})");
            #endregion
            #endregion
        }