public void Dispose() { if (_cachedEntity != null && _mediaStream != null) { // Get the new entity from the Entity Framework object state manager. ObjectStateEntry entry = this.context.ObjectStateManager.GetObjectStateEntry(_cachedEntity); if (entry.State == EntityState.Unchanged) { { var ctx = new DiscCtx(ConfigManager.ConnStr); ctx.Attach(_cachedEntity); _cachedEntity.MediaData.Data = _mediaStream.ToArray(); ctx.SaveChanges(); } } else { // A problem must have occurred when saving the entity to the database, // so we should delete the entity var mediaData = _cachedEntity.MediaData; _cachedEntity.MediaData = null; var ctx = new DiscCtx(ConfigManager.ConnStr); if (mediaData != null) { ctx.Attach(mediaData); ctx.DeleteObject(mediaData); } ctx.Attach(_cachedEntity); ctx.DeleteObject(_cachedEntity); throw new DataServiceException("An error occurred. The media resource could not be saved."); } } }
public void DeleteStream(object entity, DataServiceOperationContext operationContext) { var attachment = entity as Attachment; if (attachment == null) { throw new DataServiceException(500, "No such attachment"); } try { // Delete the requested file by using the key value. attachment.Person = null; attachment.PersonWithAvatar = null; attachment.Discussion = null; attachment.ArgPoint = null; var mediaData = attachment.MediaData; attachment.MediaData = null; if (mediaData != null) { context.DeleteObject(mediaData); } context.DeleteObject(attachment); } catch (IOException ex) { throw new DataServiceException("Error during attachment removal: ", ex); } }
public void RemoveAttachment(int attachmentId) { using (var ctx = new DiscCtx(ConfigManager.ConnStr)) { var attachment = ctx.Attachment.FirstOrDefault(at => at.Id == attachmentId); if (attachment == null) { return; } if (attachment.MediaData != null) { ctx.DeleteObject(attachment.MediaData); } ctx.DeleteObject(attachment); ctx.SaveChanges(); } }
public static void DeleteArgPoint(DiscCtx ctx, ArgPoint p) { p.Topic = null; p.Person = null; p.Comment.Clear(); p.Attachment.Clear(); p.Description = null; try { ctx.DeleteObject(p); } catch (Exception) { //if person doesn't exist, ignore } }