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