public ActionResult AddPictures(ShopPicture shopPicture, HttpPostedFileBase upload) { try { var dbShop = db.Shops.Single(s => s.Id == shopPicture.ShopId); var userId = User.Identity.GetUserId(); var shopPic = new ShopPicture { CreationDate = DateTime.UtcNow, CreateUser = db.Users.Single(u => u.Id == userId), Name = System.IO.Path.GetFileName(upload.FileName), LinkedFile = new File { FileType = FileType.ShopPicture, ContentType = upload.ContentType, }, ShopId = dbShop.Id }; // Parametre 'LeaveOpen = true' car la validation a peut etre besoin de relire la stream (sinon le Dispose() du BinaryReader efface le contenu) using (var reader = new System.IO.BinaryReader(upload.InputStream, Encoding.Default, true)) { shopPic.LinkedFile.Content = reader.ReadBytes(upload.ContentLength); } var spv = new PictureValidation <ShopPicture>(); spv.Validate(shopPicture, ModelState); spv.Validate(upload, ModelState); if (!ModelState.IsValid) { return(View(shopPicture)); } db.ShopPictures.Add(shopPic); db.SaveChanges(); } catch (DbUpdateException e) { ModelState.AddModelError("", e.Message); } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return(RedirectToAction("Details", new { Id = shopPicture.ShopId })); }