public override void Delete(TEntity entity, bool isPhysicalDelete = false) { try { TryAttach(entity); if (!isPhysicalDelete) { entity.DeletedTime = ObjHelper.ReplaceNullOrDefault(entity.LastUpdatedTime, DateTimeOffset.UtcNow); DbContext.Entry(entity).Property(x => x.DeletedTime).IsModified = true; DbContext.Entry(entity).Property(x => x.DeletedBy).IsModified = true; } else { DbSet.Remove(entity); } } catch (Exception) { RefreshEntity(entity); throw; } }
public override T Add(T entity) { entity.DeletedTime = null; entity.LastUpdatedTime = entity.CreatedTime = ObjHelper.ReplaceNullOrDefault(entity.CreatedTime, DateTimeOffset.UtcNow); entity = DbSet.Add(entity).Entity; return(entity); }
public override void Update(T entity) { TryAttach(entity); entity.LastUpdatedTime = ObjHelper.ReplaceNullOrDefault(entity.LastUpdatedTime, DateTimeOffset.UtcNow); DbContext.Entry(entity).State = EntityState.Modified; }
protected virtual void StandardizeEntities() { var listState = new List <EntityState> { EntityState.Added, EntityState.Modified }; var listEntryAddUpdate = DbContext.ChangeTracker.Entries() .Where(x => x.Entity is BaseEntity && listState.Contains(x.State)) .Select(x => x).ToList(); var dateTimeNow = DateTimeOffset.UtcNow; foreach (var entry in listEntryAddUpdate) { if (!(entry.Entity is BaseEntity entity)) { continue; } if (entry.State == EntityState.Added) { entity.DeletedTime = null; entity.LastUpdatedTime = entity.CreatedTime = ObjHelper.ReplaceNullOrDefault(entity.CreatedTime, dateTimeNow); } else { if (entity.DeletedTime != null) { entity.DeletedTime = ObjHelper.ReplaceNullOrDefault(entity.DeletedTime, dateTimeNow); } else { entity.LastUpdatedTime = ObjHelper.ReplaceNullOrDefault(entity.LastUpdatedTime, dateTimeNow); } } } }
public override void Update(T entity, params string[] changedProperties) { TryAttach(entity); changedProperties = changedProperties?.Distinct().ToArray(); entity.LastUpdatedTime = ObjHelper.ReplaceNullOrDefault(entity.LastUpdatedTime, DateTimeOffset.UtcNow); if (changedProperties?.Any() == true) { DbContext.Entry(entity).Property(x => x.LastUpdatedTime).IsModified = true; foreach (var property in changedProperties) { DbContext.Entry(entity).Property(property).IsModified = true; } } else { DbContext.Entry(entity).State = EntityState.Modified; } }
protected override void StandardizeEntities() { var listState = new List <EntityState> { EntityState.Added, EntityState.Modified, EntityState.Deleted }; var listEntry = DbContext.ChangeTracker.Entries() .Where(x => x.Entity is BaseEntity && listState.Contains(x.State)) .Select(x => x).ToList(); var dateTimeNow = GoblinDateTimeHelper.SystemTimeNow; foreach (var entry in listEntry) { if (entry.Entity is BaseEntity baseEntity) { if (entry.State == EntityState.Added) { baseEntity.DeletedTime = null; baseEntity.LastUpdatedTime = baseEntity.CreatedTime = dateTimeNow; } else { if (baseEntity.DeletedTime != null) { baseEntity.DeletedTime = ObjHelper.ReplaceNullOrDefault(baseEntity.DeletedTime, dateTimeNow); } else { baseEntity.LastUpdatedTime = dateTimeNow; } } } if (!(entry.Entity is GoblinEntity entity)) { continue; } var loggedInUserId = LoggedInUser.Current.Id; if (entry.State == EntityState.Added) { if (entity.CreatedBy == null || entity.CreatedBy == default(long)) { entity.CreatedBy = entity.LastUpdatedBy = loggedInUserId; } } else { if (entity.DeletedTime != null) { if (entity.DeletedBy == null || entity.DeletedBy == default(long)) { entity.DeletedBy = loggedInUserId; } } else { if (entity.LastUpdatedBy == null || entity.LastUpdatedBy == default(long)) { entity.LastUpdatedBy = loggedInUserId; } } } } }
protected override void StandardizeEntities() { var listState = new List <EntityState> { EntityState.Added, EntityState.Modified }; var listEntryAddUpdate = DbContext.ChangeTracker.Entries() .Where(x => x.Entity is BaseEntity && listState.Contains(x.State)) .Select(x => x).ToList(); var dateTimeNow = DateTimeOffset.UtcNow; foreach (var entry in listEntryAddUpdate) { if (entry.Entity is BaseEntity baseEntity) { if (entry.State == EntityState.Added) { baseEntity.DeletedTime = null; baseEntity.LastUpdatedTime = baseEntity.CreatedTime = ObjHelper.ReplaceNullOrDefault(baseEntity.CreatedTime, dateTimeNow); } else { if (baseEntity.DeletedTime != null) { baseEntity.DeletedTime = ObjHelper.ReplaceNullOrDefault(baseEntity.DeletedTime, dateTimeNow); } else { baseEntity.LastUpdatedTime = ObjHelper.ReplaceNullOrDefault(baseEntity.LastUpdatedTime, dateTimeNow); } } } if (!(entry.Entity is Entity entity)) { continue; } // Add more information if Entry is Entity Type Guid?loggedInUserId = null; // TODO implementation to get logged in user id if (entry.State == EntityState.Added) { entity.LastUpdatedBy = entity.CreatedBy = entity.CreatedBy; } else { if (entity.DeletedTime != null) { entity.DeletedBy = entity.DeletedBy ?? loggedInUserId; } else { entity.LastUpdatedBy = entity.LastUpdatedBy ?? loggedInUserId; } } } }