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 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)));
 }