Esempio n. 1
0
        /// <inheritdoc cref="RepositoryBase{TEntity,TDbContext}.CreateAsync(TEntity)"/>
        public override async Task <(bool success, Guid id)> CreateAsync(TrainingSession entity)
        {
            using EntityLoadLock.Releaser loadLock = EntityLoadLock.Shared.Lock();
            bool saveSuccess;

            try
            {
                EntityValidator.Validate(entity);
                DbContext.Entry(entity.TrainingRoom).State = EntityState.Unchanged;
                DbContext.Set <TrainingSession>().Add(entity);
                int saveResult = await DbContext.SaveChangesAsync();

                // Force the DbContext to fetch the species anew on next query.
                foreach (TrainingSession session in entity.TrainingRoom.TrainingSessions)
                {
                    DbContext.Entry(session).State = EntityState.Detached;
                }
                saveSuccess = Convert.ToBoolean(saveResult);
            }
            catch (Exception ex)
            {
                CreatingEntityFailedException creatingEntityFailedException = new CreatingEntityFailedException($"The entity of type {typeof(TrainingSession).Name} could not be created.", ex);
                Logger.LogError(creatingEntityFailedException, creatingEntityFailedException.Message);
                throw creatingEntityFailedException;
            }
            return(success : saveSuccess, id : entity.Id);
        }
Esempio n. 2
0
        /// <inheritdoc cref="IRepository{TEntity, TId}.CreateAsync(TEntity)"/>
        public virtual async Task <(bool success, TId id)> CreateAsync(TEntity entity)
        {
            bool saveSuccess = false;

            using EntityLoadLock.Releaser loadLock = EntityLoadLock.Shared.Lock();
            try
            {
                EntityValidator.Validate(entity);
                DbContext.Set <TEntity>().Add(entity);
                int saveResult = await DbContext.SaveChangesAsync();

                saveSuccess = Convert.ToBoolean(saveResult);
            }
            catch (Exception ex)
            {
                CreatingEntityFailedException exception = new CreatingEntityFailedException($"The entity of type {typeof(TEntity).Name} could not be created.", ex);
                Logger.Log(LogLevel.Error, exception, exception.Message);
            }
            return(success : saveSuccess, id : entity.Id);
        }
Esempio n. 3
0
        /// <inheritdoc cref="RepositoryBase{TEntity,TDbContext}.CreateAsync(TEntity)"/>
        public override async Task <(bool success, Guid id)> CreateAsync(TrainingRoom entity)
        {
            bool saveSuccess = false;

            using EntityLoadLock.Releaser loadLock = EntityLoadLock.Shared.Lock();
            try
            {
                EntityValidator.Validate(entity);
                DbContext.Entry(entity.Owner).State = EntityState.Unchanged;
                DbContext.Set <TrainingRoom>().Add(entity);
                int saveResult = await DbContext.SaveChangesAsync();

                saveSuccess = Convert.ToBoolean(saveResult);
            }
            catch (Exception ex)
            {
                CreatingEntityFailedException creatingEntityFailedException = new CreatingEntityFailedException($"The entity of type {typeof(TrainingRoom).Name} could not be created.", ex);
                Logger.LogError(creatingEntityFailedException, creatingEntityFailedException.Message);
            }
            return(saveSuccess, entity.Id);
        }