public async Task <ActionResult <OVEAssetModel> > Edit(string id, [Bind("Project,Name,Description,Id,StorageLocation,Processed,Service,AssetMeta,LastModified")] OVEAssetModel oveAssetModel, [FromForm] IFormFile upload) { if (id != oveAssetModel.Id) { return(NotFound()); } var oldAssetModel = await _context.AssetModels.FirstOrDefaultAsync(m => m.Id == id); if (oldAssetModel == null) { return(NotFound()); } // check someone is not tampering with the service after upload if (upload == null && !_serviceRepository.ValidateServiceChoice(oveAssetModel.Service, oveAssetModel.StorageLocation)) { ModelState.AddModelError("Service", "Service does not support File Type"); } // concurrent fields need to be updated by themselves and atomically. bool need2UpdateProcessingState = oldAssetModel.ProcessingState != oveAssetModel.ProcessingState; if (need2UpdateProcessingState) { oveAssetModel.ProcessingState = oldAssetModel.ProcessingState; } if (ModelState.IsValid) { try { if (oldAssetModel.Project != oveAssetModel.Project) { await _fileOperations.Move(oldAssetModel, oveAssetModel); // todo not implemented } //stop EF from tracking the old version so that it will allow you to update the new version _context.Entry(oldAssetModel).State = EntityState.Detached; if (upload != null && upload.Length > 0) { if (!_serviceRepository.ValidateServiceChoice(oveAssetModel.Service, upload)) { ModelState.AddModelError("Service", "Service does not support File Type"); } if (!await _fileOperations.Delete(oveAssetModel)) { return(UnprocessableEntity("unable to delete old file")); } if (!await _fileOperations.Save(oveAssetModel, upload)) { return(UnprocessableEntity("unable to save new file")); } need2UpdateProcessingState = true; } oveAssetModel.LastModified = DateTime.Now; _context.Update(oveAssetModel); await _context.SaveChangesAsync(); if (need2UpdateProcessingState) { oveAssetModel.ProcessingState = 0; _context.Update(oveAssetModel); await _context.SaveChangesAsync(); } } catch (DbUpdateConcurrencyException) { if (!_context.AssetModels.Any(e => e.Id == oveAssetModel.Id)) { return(NotFound()); } _logger.LogError("Concurrency Error updating database "); return(Conflict()); } return(RedirectToAction(nameof(Index))); } return(this.FormatOrView(oveAssetModel)); }