title: Restore database from backup in asp mvc
body: <p>i am new in asp mvc and i am trying to make actions that backup database and restore it from the backup file</p>
  <p>here the code</p>
  <pre><code>public class BackupController : Controller
  {
      private RemasEntities1 db = new RemasEntities1();
      // GET: Backup
      public ActionResult BackupDatabase()
      {
          string dbPath = Server.MapPath(&quot;~/App_Data/DBBackup.bak&quot;);
          using (var db = new RemasEntities1())
          {
              var cmd = String.Format(&quot;BACKUP DATABASE {0} TO DISK='{1}' WITH FORMAT, MEDIANAME='DbBackups', MEDIADESCRIPTION='Media set for {0} database';&quot;
                  , &quot;Remas&quot;, dbPath);
              db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, cmd);
          }
          return new FilePathResult(dbPath, &quot;application/octet-stream&quot;);
      }
  
     
      public ActionResult RestoreDatabase()
      {
          string dbPath = Server.MapPath(&quot;~/App_Data/DBBackup.bak&quot;);
          using (var db = new RemasEntities1())
          {
  
              var cmd = String.Format(&quot;restore DATABASE {0} from DISK='{1}';&quot;
                  , &quot;Remas&quot;, dbPath);
              db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, cmd);
          }
          return View();
      }
  }
  </code></pre>
  <p>the backup action works good, but when i try to restore database from restore action, it throw an exception</p>
  <blockquote>
  <p>An exception of type sql.data.sqlexception' occured in entityframework.sqlserver.dll but was not handled in user code</p>
  <p>Restore cannot process database 'Remas' because it is in use by this session. It is recommended that the master database be used when performing this operation</p>
  </blockquote>