Cannot perform backup or restore operation inside a transaction. BACKUP DATABASE ends abnormally

Hi guys, I'm trying to execute the backUp and restore command in my application, which she modeled using EF6

For my backup, I create Sp , as you can see here:

 CREATE PROCEDURE GetBackUp -- Add the parameters for the stored procedure here @address nvarchar(max) AS BEGIN BACKUP DATABASE [db-invoice-169] to DISK=@address END GO 

In my application, I call it Sp

  private void Backup_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); string str_filename = string.Empty; sfd.FileName = "backup_database_" + DateTime.Now.ToShortDateString().Replace("/", "_"); sfd.Filter = @"backup files(*.bak)|*.bak|all files(*.*)|*.*"; sfd.FilterIndex = 1; sfd.OverwritePrompt = true; sfd.Title = "***save backup files***"; if (sfd.ShowDialog() == DialogResult.OK) { str_filename = sfd.FileName; backup(str_filename); } } private void backup(string str_filename) { try { this.Cursor = Cursors.WaitCursor; db.Database.ExecuteSqlCommand(@"EXEC [dbo].[back_up] @address = N'"+str_filename+"'"); this.Cursor = Cursors.Default; MessageBox.Show("عملیات پشتیبان گیری موفقیت آمیز بود"); } catch (Exception ex) { MessageBox.Show("عملیات پشتیبان گیری موفقیت آمیز نبود |" + ex.Message); } } 

But I got this error:

 Cannot perform a backup or restore operation within a transaction. BACKUP DATABASE is terminating abnormally. 
+5
source share
1 answer

Try changing this line:

 db.Database.ExecuteSqlCommand(@"EXEC [dbo].[back_up] @address = N'"+str_filename+"'"); 

in

 db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, @"EXEC [dbo].[back_up] @address = N'"+str_filename+"'"); 

Abrar

+16
source

Source: https://habr.com/ru/post/1200329/


All Articles