/// <summary> /// Step 1 /// - Init Culture /// - Init System pages /// </summary> /// <param name="siteName"></param> /// <param name="culture"></param> /// <returns></returns> public static async Task <RepositoryResponse <bool> > InitCms(string siteName, InitCulture culture) { RepositoryResponse <bool> result = new RepositoryResponse <bool>(); MixCmsContext context = null; MixCmsAccountContext accountContext = null; MixChatServiceContext messengerContext = null; try { if (!string.IsNullOrEmpty(MixService.GetConnectionString(MixConstants.CONST_CMS_CONNECTION))) { context = MixService.GetDbContext(); accountContext = new MixCmsAccountContext(); messengerContext = new MixChatServiceContext(); await context.Database.MigrateAsync(); await accountContext.Database.MigrateAsync(); await messengerContext.Database.MigrateAsync(); var countCulture = context.MixCulture.Count(); var pendingMigration = context.Database.GetPendingMigrations().Count(); if (pendingMigration == 0) { return(await InitSiteData(siteName, culture)); } } return(result); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { result.IsSucceed = false; result.Exception = ex; return(result); } finally { context?.Dispose(); accountContext?.Dispose(); messengerContext?.Dispose(); } }
public static async Task <RepositoryResponse <bool> > InitSiteData(string siteName, InitCulture culture) { RepositoryResponse <bool> result = new RepositoryResponse <bool>(); MixCmsContext context = null; IDbContextTransaction transaction = null; bool isSucceed = true; try { if (!string.IsNullOrEmpty(MixService.GetConnectionString(MixConstants.CONST_CMS_CONNECTION))) { context = MixService.GetDbContext(); transaction = context.Database.BeginTransaction(); var countCulture = context.MixCulture.Count(); /** * Init Selected Language as default */ isSucceed = InitCultures(culture, context, transaction); /** * Init System Configurations */ if (isSucceed && context.MixConfiguration.Count() == 0) { var saveResult = await InitConfigurationsAsync(siteName, culture.Specificulture, context, transaction); result.IsSucceed = saveResult.IsSucceed; result.Errors = saveResult.Errors; result.Exception = saveResult.Exception; } else { result.IsSucceed = false; result.Errors.Add("Cannot init cultures"); } if (result.IsSucceed) { transaction.Commit(); } else { transaction.Rollback(); } } return(result); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { transaction?.Rollback(); result.IsSucceed = false; result.Exception = ex; return(result); } finally { context?.Database.CloseConnection(); context?.Dispose(); } }