public virtual void Delete(TEntity entityToDelete) { using (var db = _dbFactory.Create()) { var dbSet = db.Set <TEntity>(); if (db.Entry(entityToDelete).State == EntityState.Detached) { dbSet.Attach(entityToDelete); } var entity = entityToDelete as IDeleteEntity; if (entity != null) { var deleteEntity = entity; deleteEntity.IsDeleted = true; deleteEntity.TimeDeleteOnUtc = DateTime.UtcNow; var currentUser = CustomPrincipal.GetCurrentUser(); if (currentUser != null) { deleteEntity.DeleteBy = currentUser.Id; } db.SaveChanges(); return; } dbSet.Remove(entityToDelete); db.SaveChanges(); } }