/// <summary> /// Determines whether the database schema is up to date. /// </summary> /// <returns> /// <c>true</c> if [is database up to date]; otherwise, <c>false</c>. /// </returns> public bool IsDatabaseUpToDate() { using (var installationContext = new EntityDataContext()) { return(!installationContext.Database.GetPendingMigrations().Any()); } }
/// <summary> /// Applies all pending updates to the database schema. /// </summary> public void ApplyAllPendingUpdates() { using (var installationContext = new EntityDataContext()) { installationContext.Database.Migrate(); } }
/// <inheritdoc/> public bool GuildWithNameExists(string name) { using (var context = new EntityDataContext()) { return(context.Set <Guild>().Any(guild => guild.Name == name)); } }
/// <summary> /// Initializes a new instance of the <see cref="GenericRepositoryBase{T}" /> class. /// </summary> /// <param name="repositoryManager">The repository manager.</param> /// <param name="logger">The logger.</param> protected GenericRepositoryBase(RepositoryManager repositoryManager, ILogger logger) { this.logger = logger; this.RepositoryManager = repositoryManager; using var completeContext = new EntityDataContext(); this.FullEntityType = completeContext.Model.FindEntityType(typeof(T)); }
/// <summary> /// Determines if the database exists already, by checking if any migration has been applied. /// </summary> /// <returns><c>True</c>, if the database exists; Otherwise, <c>false</c>.</returns> public bool DatabaseExists() { using (var installationContext = new EntityDataContext()) { return(installationContext.Database.GetAppliedMigrations().Any()); } }
/// <summary> /// Reinitializes the database by deleting it and running the initialization process again. /// </summary> public void ReInitializeDatabase() { using (var installationContext = new EntityDataContext()) { installationContext.Database.EnsureDeleted(); installationContext.Database.Migrate(); } }
/// <summary> /// Reinitializes the database by deleting it and running the initialization process again. /// </summary> public void ReInitializeDatabase() { using (var installationContext = new EntityDataContext()) { installationContext.Database.EnsureDeleted(); this.ApplyAllPendingUpdates(); } }
/// <summary> /// Initializes a new instance of the <see cref="GenericRepository{T}"/> class. /// </summary> /// <param name="contextProvider">The repository manager.</param> public GenericRepository(PersistenceContextProvider contextProvider) { this.contextProvider = contextProvider; using (var completeContext = new EntityDataContext()) { this.fullEntityType = completeContext.Model.FindEntityType(typeof(T)); } }
/// <summary> /// Initializes a new instance of the <see cref="GenericRepository{T}"/> class. /// </summary> /// <param name="repositoryManager">The repository manager.</param> public GenericRepository(IRepositoryManager repositoryManager) { this.repositoryManager = repositoryManager; using (var completeContext = new EntityDataContext()) { this.fullEntityType = completeContext.Model.FindEntityType(typeof(T)); } }
/// <inheritdoc/> public FriendViewItem CreateNewFriendViewItem(string characterName, string friendName) { var item = this.repositoryManager.CreateNew <FriendViewItem>(); using (var context = new EntityDataContext()) { item.CharacterId = context.Set <Character>().Where(character => character.Name == characterName).Select(character => character.Id).FirstOrDefault(); item.FriendId = context.Set <Character>().Where(character => character.Name == friendName).Select(character => character.Id).FirstOrDefault(); } // The names are available after commit and are not allowed to be set before. return(item); }
/// <summary> /// Applies all pending updates to the database schema. /// </summary> public void ApplyAllPendingUpdates() { using (var installationContext = new EntityDataContext()) { var isNewDatabase = !installationContext.Database.GetAppliedMigrations().Any(); installationContext.Database.Migrate(); if (isNewDatabase) { var dataInitialization = new DataInitialization(this); dataInitialization.CreateInitialData(); } } }
/// <summary> /// Registers generic repositories for all types of the data model which are not registered otherwise. /// </summary> protected void RegisterMissingRepositoriesAsGeneric() { var registeredTypes = this.Repositories.Keys.ToList(); using var entityContext = new EntityDataContext(); var modelTypes = entityContext.Model.GetEntityTypes().Select(e => e.ClrType); var missingTypes = modelTypes.Where(t => !registeredTypes.Contains(t.BaseType)); foreach (var type in missingTypes) { var repository = this.CreateGenericRepository(type); this.RegisterRepository(type, repository); } }
/// <summary> /// Registers the repositories. /// </summary> /// <param name="contextProvider">The context provider which is required in most of the repositories.</param> public void RegisterRepositories(PersistenceContextProvider contextProvider) { this.RegisterRepository(new AccountRepository(contextProvider)); this.RegisterRepository(new LetterBodyRepository(contextProvider)); this.RegisterRepository(new CachedRepository <GameConfiguration>(new GameConfigurationRepository(contextProvider))); this.RegisterRepository(new GenericRepository <GameServerConfiguration>(contextProvider)); this.RegisterRepository(new GenericRepository <GameClientDefinition>(contextProvider)); this.RegisterRepository(new GenericRepository <ConnectServerDefinition>(contextProvider)); this.RegisterRepository(new GenericRepository <ChatServerDefinition>(contextProvider)); this.RegisterRepository(new GenericRepository <ChatServerEndpoint>(contextProvider)); this.RegisterRepository(new GenericRepository <GameServerEndpoint>(contextProvider)); this.RegisterRepository(new GameServerDefinitionRepository(contextProvider)); this.RegisterRepository(new ConfigurationTypeRepository <ItemOptionDefinition>(contextProvider, config => config.RawItemOptions)); this.RegisterRepository(new ConfigurationTypeRepository <IncreasableItemOption>( contextProvider, config => config.RawItemOptions.SelectMany(o => o.RawPossibleOptions).Concat(config.RawItemSetGroups.SelectMany(g => g.RawOptions)).Distinct().ToList())); this.RegisterRepository(new ConfigurationTypeRepository <AttributeDefinition>(contextProvider, config => config.RawAttributes)); this.RegisterRepository(new ConfigurationTypeRepository <DropItemGroup>(contextProvider, config => config.RawBaseDropItemGroups)); this.RegisterRepository(new ConfigurationTypeRepository <CharacterClass>(contextProvider, config => config.RawCharacterClasses)); this.RegisterRepository(new ConfigurationTypeRepository <ItemOptionType>(contextProvider, config => config.RawItemOptionTypes)); this.RegisterRepository(new ConfigurationTypeRepository <ItemSetGroup>(contextProvider, config => config.RawItemSetGroups)); this.RegisterRepository(new ConfigurationTypeRepository <ItemSlotType>(contextProvider, config => config.RawItemSlotTypes)); this.RegisterRepository(new ConfigurationTypeRepository <ItemDefinition>(contextProvider, config => config.RawItems)); this.RegisterRepository(new ConfigurationTypeRepository <JewelMix>(contextProvider, config => config.RawJewelMixes)); this.RegisterRepository(new ConfigurationTypeRepository <MagicEffectDefinition>(contextProvider, config => config.RawMagicEffects)); this.RegisterRepository(new ConfigurationTypeRepository <GameMapDefinition>(contextProvider, config => config.RawMaps)); this.RegisterRepository(new ConfigurationTypeRepository <MasterSkillRoot>(contextProvider, config => config.RawMasterSkillRoots)); this.RegisterRepository(new ConfigurationTypeRepository <MonsterDefinition>(contextProvider, config => config.RawMonsters)); this.RegisterRepository(new ConfigurationTypeRepository <Skill>(contextProvider, config => config.RawSkills)); this.RegisterRepository(new ConfigurationTypeRepository <PlugInConfiguration>(contextProvider, config => config.RawPlugInConfigurations)); var registeredTypes = this.Repositories.Keys.ToList(); using (var entityContext = new EntityDataContext()) { var modelTypes = entityContext.Model.GetEntityTypes().Select(e => e.ClrType); var missingTypes = modelTypes.Where(t => !registeredTypes.Contains(t.BaseType) && !(t.BaseType?.Namespace?.Contains("Configuration") ?? false)).ToList(); foreach (var type in missingTypes) { Log.Debug($"No repository registered for type {type}, creating a generic one."); var repositoryType = typeof(GenericRepository <>).MakeGenericType(type); var repository = Activator.CreateInstance(repositoryType, contextProvider) as IRepository; this.RegisterRepository(type, repository); } } }
/// <summary> /// Registers the repositories. /// </summary> public void RegisterRepositories() { this.RegisterRepository(new AccountRepository(this)); this.RegisterRepository(new GuildRepository(this)); this.RegisterRepository(new FriendViewItemRepository(this)); this.RegisterRepository(new CachedRepository <GameConfiguration>(new GameConfigurationRepository(this))); this.RegisterRepository(new GenericRepository <GameServerConfiguration>(this)); this.RegisterRepository(new GameServerDefinitionRepository(this)); this.RegisterRepository(new GenericRepository <MainPacketHandlerConfiguration>(this)); this.RegisterRepository(new GenericRepository <PacketHandlerConfiguration>(this)); this.RegisterRepository(new ConfigurationTypeRepository <ItemOptionDefinition>(this, config => config.RawItemOptions)); this.RegisterRepository(new ConfigurationTypeRepository <AttributeDefinition>(this, config => config.RawAttributes)); this.RegisterRepository(new ConfigurationTypeRepository <DropItemGroup>(this, config => config.RawBaseDropItemGroups)); this.RegisterRepository(new ConfigurationTypeRepository <CharacterClass>(this, config => config.RawCharacterClasses)); this.RegisterRepository(new ConfigurationTypeRepository <ItemOptionType>(this, config => config.RawItemOptionTypes)); this.RegisterRepository(new ConfigurationTypeRepository <ItemSetGroup>(this, config => config.RawItemSetGroups)); this.RegisterRepository(new ConfigurationTypeRepository <ItemSlotType>(this, config => config.RawItemSlotTypes)); this.RegisterRepository(new ConfigurationTypeRepository <ItemDefinition>(this, config => config.RawItems)); this.RegisterRepository(new ConfigurationTypeRepository <JewelMix>(this, config => config.RawJewelMixes)); this.RegisterRepository(new ConfigurationTypeRepository <MagicEffectDefinition>(this, config => config.RawMagicEffects)); this.RegisterRepository(new GameMapDefinitionRepository(this, config => config.RawMaps)); this.RegisterRepository(new ConfigurationTypeRepository <MasterSkillRoot>(this, config => config.RawMasterSkillRoots)); this.RegisterRepository(new ConfigurationTypeRepository <MonsterDefinition>(this, config => config.RawMonsters)); this.RegisterRepository(new ConfigurationTypeRepository <Skill>(this, config => config.RawSkills)); var registeredTypes = this.Repositories.Keys.ToList(); using (var entityContext = new EntityDataContext()) { var modelTypes = entityContext.Model.GetEntityTypes().Select(e => e.ClrType); var missingTypes = modelTypes.Where(t => !registeredTypes.Contains(t.BaseType) && !(t.BaseType?.Namespace?.Contains("Configuration") ?? false)).ToList(); foreach (var type in missingTypes) { Log.Debug($"No repository registered for type {type}, creating a generic one."); var repositoryType = typeof(GenericRepository <>).MakeGenericType(type); var repository = Activator.CreateInstance(repositoryType, this as IRepositoryManager) as IRepository; this.RegisterRepository(type, repository); } } }
/// <summary> /// Recreates the database by deleting and creating it again. /// </summary> public void ReCreateDatabase() { using var installationContext = new EntityDataContext(); installationContext.Database.EnsureDeleted(); this.ApplyAllPendingUpdates(); }