protected virtual void TrackSingle(BaseEntity entity, int mediaFileId, MediaTrackOperation operation) { Guard.NotNull(entity, nameof(entity)); if (mediaFileId < 1 || entity.IsTransientRecord()) { return; } var file = _dbContext.Set <MediaFile>().Find(mediaFileId); if (file != null) { var albumName = _folderService.FindAlbum(file)?.Value.Name; if (albumName.IsEmpty()) { throw new InvalidOperationException("Cannot track a media file that is not assigned to any album."); } var track = new MediaTrack { EntityId = entity.Id, EntityName = entity.GetEntityName(), MediaFileId = mediaFileId, Album = albumName }; if (operation == MediaTrackOperation.Track) { file.Tracks.Add(track); } else { var dbTrack = file.Tracks.FirstOrDefault(x => x == track); if (dbTrack != null) { file.Tracks.Remove(track); _dbContext.ChangeState(dbTrack, System.Data.Entity.EntityState.Deleted); } } if (file.Tracks.Count > 0) { // A file with tracks can NEVER be transient file.IsTransient = false; } else if (_makeFilesTransientWhenOrphaned) { // But an untracked file can OPTIONALLY be transient file.IsTransient = true; } _dbContext.SaveChanges(); } }
/// <summary> /// Changes the object state to unchanged /// </summary> /// <typeparam name="TEntity">Type of entity</typeparam> /// <param name="entity">The entity instance</param> /// <returns>true on success, false on failure</returns> public static bool SetToUnchanged <TEntity>(this IDbContext ctx, TEntity entity) where TEntity : BaseEntity, new() { try { ctx.ChangeState <TEntity>(entity, EntityState.Unchanged); return(true); } catch (Exception ex) { ex.Dump(); return(false); } }
public void Save(MediaItem media, byte[] data) { Guard.NotNull(media, nameof(media)); if (data == null || data.LongLength == 0) { // remove media storage if any if ((media.Entity.MediaStorageId ?? 0) != 0 && media.Entity != null && media.Entity.MediaStorage != null) { _mediaStorageRepository.Delete(media.Entity.MediaStorage); } } else { if (media.Entity.MediaStorage == null) { // entity has no media storage -> insert var newStorage = new MediaStorage { Data = data }; _mediaStorageRepository.Insert(newStorage); if (newStorage.Id == 0) { // actually we should never get here _dbContext.SaveChanges(); } media.Entity.MediaStorageId = newStorage.Id; // Required because during import the ChangeTracker doesn't treat media.Entity as changed entry. _dbContext.ChangeState((BaseEntity)media.Entity, System.Data.Entity.EntityState.Modified); _dbContext.SaveChanges(); } else { // update existing media storage media.Entity.MediaStorage.Data = data; _mediaStorageRepository.Update(media.Entity.MediaStorage); } } }
protected virtual void TrackSingle(BaseEntity entity, int mediaFileId, string propertyName, MediaTrackOperation operation) { Guard.NotNull(entity, nameof(entity)); if (mediaFileId < 1 || entity.IsTransientRecord()) { return; } var file = _dbContext.Set <MediaFile>().Find(mediaFileId); if (file != null) { var album = _folderService.FindAlbum(file)?.Value; if (album == null) { throw new InvalidOperationException(T("Admin.Media.Exception.TrackUnassignedFile")); } else if (!album.CanDetectTracks) { // No support for tracking on album level, so get outta here. return; } var track = new MediaTrack { EntityId = entity.Id, EntityName = entity.GetEntityName(), MediaFileId = mediaFileId, Property = propertyName, Album = album.Name }; if (operation == MediaTrackOperation.Track) { file.Tracks.Add(track); } else { var dbTrack = file.Tracks.FirstOrDefault(x => x == track); if (dbTrack != null) { file.Tracks.Remove(track); _dbContext.ChangeState(dbTrack, System.Data.Entity.EntityState.Deleted); } } if (file.Tracks.Count > 0) { // A file with tracks can NEVER be transient file.IsTransient = false; } else if (_makeFilesTransientWhenOrphaned) { // But an untracked file can OPTIONALLY be transient file.IsTransient = true; } _dbContext.SaveChanges(); } }
public void UpdateOnCommit(T entity) { _context.ChangeState(entity, EntityState.Modified); }