public virtual void Update(T entity)
 {
     AppLogger.logDebug(this.ToString(), string.Format("Begin updating entity of {0}.", typeof(T)));
     try
     {
         AppLogger.logDebug(this.ToString(), "UPDATE", entity);
         if (entity.EntityState == EntityState.Detached)
         {
             var id             = entity.GetType().GetProperty("id").GetValue(entity, null).ToString();
             var attachedEntity = EntitiesSet.AsEnumerable().FirstOrDefault(e => e.EntityKey.EntityKeyValues[0].Value.Equals(id));
             if (attachedEntity != null)
             {
                 // HACK: Do the trick to detach the existed entity and attach the new entity, then apply the original values
                 // from the old entity to the new entity. This will set the new entity's state to "Modified" and the
                 // ObjectStateManager will track only different values.
                 EntitiesSet.Detach(attachedEntity);
                 EntitiesSet.Attach(entity);
                 var entry = Context.ObjectStateManager.GetObjectStateEntry(entity);
                 entry.ApplyOriginalValues(attachedEntity);
             }
         }
         AppLogger.logDebug(this.ToString(), string.Format("Entity of {0} updated successfully.", typeof(T)));
     }
     catch (Exception ex)
     {
         AppLogger.logError(this.ToString(), string.Format("Error occurs while updating entity of {0}.", typeof(T)), ex);
         throw;
     }
     AppLogger.logDebug(this.ToString(), string.Format("Finish updating entity of {0}.", typeof(T)));
 }
 public virtual void Delete(Expression <Func <T, bool> > predicate)
 {
     AppLogger.logDebug(this.ToString(), string.Format("Begin deleting entities of {0}.", typeof(T)));
     AppLogger.logDebug(this.ToString(), string.Format("Expression: '{0}'.", predicate.Body.ToString()));
     try
     {
         if (predicate != null)
         {
             IEnumerable <T> entities = EntitiesSet.Where(predicate);
             AppLogger.logDebug(this.ToString(), string.Format("Count: {0}", entities.Count()));
             foreach (T entity in entities)
             {
                 AppLogger.logDebug(this.ToString(), "DELETE", entity);
                 Context.DeleteObject(entity);
             }
             AppLogger.logDebug(this.ToString(), string.Format("Deleted entities of {0} successfully.", typeof(T)));
         }
     }
     catch (Exception ex)
     {
         AppLogger.logError(this.ToString(), string.Format("Error occurs while deleting entities of {0}. Predicate: '{1}'.", typeof(T), predicate.Body.ToString()), ex);
         throw;
     }
     AppLogger.logDebug(this.ToString(), string.Format("Finish deleting entities of {0}.", typeof(T)));
 }
 public virtual void InsertOrUpdate(IEnumerable <T> entities)
 {
     AppLogger.logDebug(this.ToString(), string.Format("Begin inserting or updating entities of {0}. Count: {1}.", typeof(T), entities.Count()));
     try
     {
         foreach (T entity in entities)
         {
             AppLogger.logDebug(this.ToString(), "INSERT OR UPDATE", entity);
             string id             = entity.GetType().GetProperty("id").GetValue(entity, null).ToString();
             var    attachedEntity = EntitiesSet.AsEnumerable().FirstOrDefault(e => e.EntityKey.EntityKeyValues[0].Value.Equals(id));
             if (attachedEntity != null)
             {
                 EntitiesSet.Detach(attachedEntity);
                 EntitiesSet.Attach(entity);
                 var entry = Context.ObjectStateManager.GetObjectStateEntry(entity);
                 entry.ApplyOriginalValues(attachedEntity);
             }
             else
             {
                 EntitiesSet.AddObject(entity);
             }
         }
         AppLogger.logDebug(this.ToString(), string.Format("Entities of {0} inserted or updated successfully.", typeof(T)));
     }
     catch (Exception ex)
     {
         AppLogger.logError(this.ToString(), string.Format("Error occurs while inserting or updating entities of {0}.", typeof(T)), ex);
         throw;
     }
     AppLogger.logDebug(this.ToString(), string.Format("Finish inserting or updating entities of {0}.", typeof(T)));
 }
        public virtual T Get(Func <T, bool> predicate)
        {
            T entity = null;

            AppLogger.logDebug(this.ToString(), string.Format("Begin getting entity of {0}.", typeof(T)));
            try
            {
                if (predicate != null)
                {
                    entity = EntitiesSet.FirstOrDefault(predicate);
                    if (entity != null)
                    {
                        AppLogger.logDebug(this.ToString(), string.Format("Entity of {0} found.", typeof(T)));
                        AppLogger.logDebug(this.ToString(), "GET", entity);
                        EntitiesSet.Detach(entity);
                    }
                    else
                    {
                        AppLogger.logDebug(this.ToString(), string.Format("Entity of {0} not found.", typeof(T)));
                    }
                }
            }
            catch (Exception ex)
            {
                AppLogger.logError(this.ToString(), ex);
                throw;
            }
            AppLogger.logDebug(this.ToString(), string.Format("Finish getting entity of {0}.", typeof(T)));
            return(entity);
        }
        public async Task <LevelTest> GetOneWithQuestionsAndAnswerOptions(int id)
        {
            var levelTest = await EntitiesSet.Where(t => t.Id == id && t.IsDeleted != true)
                            .Include(t => t.Questions)
                            .ThenInclude(q => q.AnswerOptions).FirstOrDefaultAsync();

            return(levelTest);
        }
Example #6
0
        public async Task <IEnumerable <Comment> > GetManyWithSubComment(Expression <Func <Comment, bool> > condition)
        {
            var comments = await EntitiesSet.Where(condition)
                           .Include(c => c.SubComments)
                           .ToListAsync();

            return(comments);
        }
        public async Task <IEnumerable <LevelTest> > GetAllWithQuestionsAndOptions()
        {
            var levelTests = await EntitiesSet.Where(t => t.IsDeleted != true)
                             .Include(t => t.Questions)
                             .ThenInclude(q => q.AnswerOptions).ToListAsync();

            return(levelTests);
        }
Example #8
0
        public TDbEntity Create()
        {
#if EF6
            return(EntitiesSet.Create());
#else
            throw new NotImplementedException();
#endif
        }
Example #9
0
        public async Task <Comment> GetOneWithSubComment(Expression <Func <Comment, bool> > condition)
        {
            var comment = await EntitiesSet.Where(condition)
                          .Include(c => c.SubComments)
                          .FirstOrDefaultAsync();

            return(comment);
        }
Example #10
0
 public void AddOrUpdate(TDbEntity dbEntity, bool @new = false)
 {
     if (@new)
     {
         EntitiesSet.Add(dbEntity);
     }
     else
     {
         Context.Entry(dbEntity).State = EntityState.Modified;
     }
 }
 public virtual void Insert(T entity)
 {
     AppLogger.logDebug(this.ToString(), string.Format("Begin inserting entity of {0}.", typeof(T)));
     try
     {
         AppLogger.logDebug(this.ToString(), "INSERT", entity);
         EntitiesSet.AddObject(entity);
         AppLogger.logDebug(this.ToString(), string.Format("Entity of {0} inserted successfully.", typeof(T)));
     }
     catch (Exception ex)
     {
         AppLogger.logError(this.ToString(), string.Format("Error occurs while inserting entity of {0}.", typeof(T)), ex);
         throw;
     }
     AppLogger.logDebug(this.ToString(), string.Format("Finish inserting entity of {0}.", typeof(T)));
 }
 public virtual void Delete(IEnumerable <T> entities)
 {
     AppLogger.logDebug(this.ToString(), string.Format("Begin deleting entities of {0}. Count: {1}.", typeof(T), entities.Count()));
     try
     {
         foreach (T entity in entities)
         {
             AppLogger.logDebug(this.ToString(), "DELETE", entity);
             EntitiesSet.Attach(entity);
             Context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Deleted);
         }
         AppLogger.logDebug(this.ToString(), string.Format("Entities of {0} deleted successfully.", entities.GetType()));
     }
     catch (Exception ex)
     {
         AppLogger.logError(this.ToString(), string.Format("Error occurs while deleting entities of {0}.", typeof(T)), ex);
         throw;
     }
     AppLogger.logDebug(this.ToString(), string.Format("Finish deleting entities of {0}.", typeof(T)));
 }
 public virtual void Delete(T entity)
 {
     AppLogger.logDebug(this.ToString(), string.Format("Begin deleting entity of {0}.", typeof(T)));
     try
     {
         if (entity != null)
         {
             AppLogger.logDebug(this.ToString(), "DELETE", entity);
             EntitiesSet.Attach(entity);
             Context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Deleted);
             AppLogger.logDebug(this.ToString(), string.Format("Entity of {0} deleted successfully.", typeof(T)));
         }
         else
         {
             AppLogger.logDebug(this.ToString(), string.Format("Entity of {0} not found.", typeof(T)));
         }
     }
     catch (Exception ex)
     {
         AppLogger.logError(this.ToString(), string.Format("Error occurs while deleting entity of {0}.", typeof(T)), ex);
         throw;
     }
     AppLogger.logDebug(this.ToString(), string.Format("Finish deleting entity of {0}.", typeof(T)));
 }
 public TDbEntity FindById(params object[] id)
 {
     return(EntitiesSet.Find(id));
 }
Example #15
0
 public IQueryable <File> FilesByIds(IEnumerable <Guid> ids)
 {
     return(EntitiesSet.Where(x => ids.Contains(x.Id)));
 }
Example #16
0
 public void Delete(TDbEntity dbEntity)
 {
     EntitiesSet.Remove(dbEntity);
 }
Example #17
0
 public void Add(IEnumerable <TDbEntity> dbEntities)
 {
     EntitiesSet.AddRange(dbEntities);
 }
 public IQueryable <TDbEntity> Find(Expression <Func <TDbEntity, bool> > filter = null)
 {
     return(filter == null ? EntitiesSet : EntitiesSet.Where(filter));
 }
Example #19
0
        public void DeleteMany(IEnumerable <TDbEntity> dbEntities)
        {
            var entities = dbEntities.ToArray();

            EntitiesSet.RemoveRange(entities);
        }
Example #20
0
 public async Task <TDbEntity> FindByIdAsync(int id)
 {
     return(await EntitiesSet.FindAsync(id).ConfigureAwait(false));
 }
Example #21
0
 public void Add(TDbEntity dbEntity)
 {
     EntitiesSet.Add(dbEntity);
 }
Example #22
0
 public TDbEntity FindById(int id)
 {
     return(EntitiesSet.Find(id));
 }
Example #23
0
 public bool Any(Expression <Func <TDbEntity, bool> > predicate)
 {
     return(EntitiesSet.Any(predicate));
 }
        public async Task <Message> GetLastedMessage(int conversationId)
        {
            var message = await EntitiesSet.Where(m => m.ConversationId == conversationId).OrderByDescending(s => s.CreatedDate).FirstOrDefaultAsync();

            return(message);
        }
Example #25
0
 public void Delete(IEnumerable <TDbEntity> dbEntities)
 {
     EntitiesSet.RemoveRange(dbEntities);
 }
Example #26
0
 public Task <TDbEntity> FindByIdAsync(Guid id)
 {
     return(EntitiesSet.FindAsync(id));
 }