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