title: ASP.NET MVC - EF Stored procedure and file upload. No roll-back
body: <p>I have a problem when I'm using insert stored procedures and uploading a file in ASP.NET MVC.<br/></p>
  
  <p>I have three parts in my try-catch block:</p>
  
  <ol>
  <li>Generate the file name.</li>
  <li>Executing the insert stored procedure. </li>
  <li>Saving the file.</li>
  </ol>
  
  <p>Generate a file name, insert that file name into the database and save a file with that name.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>If executing the insert <strong>stored procedure succeeds</strong> and <strong>file save fails</strong>, the inserted row still remains resulting in the event without the photo.</p>
  
  <p>If I switch the order, first saving the file and then inserting into the database, the problem still remains. If <strong>file save succeed</strong>s and <strong>insert fails</strong>, there is a photo without the event.</p>
  
  <p>Sample code (just an illustration).</p>
  
  <pre><code>try
  {
      //Generate a new file name.
      var newFileName = Guid.NewGuid().ToString();
  
      //Execute stored procedore.
      Database.Insert_Event(newFileName, title, description, dateTime);
  
      //Save event photo.
      eventPhoto.SaveAs(newFileName);
  }
  catch (DataException exception)
  {
      //Exception message when executing insert SP.
  }
  catch (IOException exception)
  {
      //Exception message when saving file.
  }
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>How to revert the database insert when inserting using stored procedure if the file save fails or how to revert file save if the insert fails?</p>