backup sql db whit entity framework

hi
i have this sp :

USE [CarsDB]
GO
/****** Object:  StoredProcedure [dbo].[EXEC_BACKUP]    Script Date: 05/05/2015 20:38:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[EXEC_BACKUP]
      ( @BACKUPPATH VARCHAR(250),
  @BKPDATABASE VARCHAR(100))
AS BEGIN
   DECLARE @@STRQUERY NVARCHAR(500)

   SET @@STRQUERY = 'BACKUP DATABASE ' + @BKPDATABASE + ' TO DISK = '
                    + QUOTENAME(@BACKUPPATH, '''');
   EXECUTE SP_EXECUTESQL @@STRQUERY
 
END;

and from c# form i am running this code to execute the sp :
   public static void DoMakeGib()
        {

            try
            {
                using (EF.CarsDBEntities ef = new EF.CarsDBEntities())
                {
                   
                    string FilePath = "C:\\CarsDB.bak";
                    ef.EXEC_BACKUP(FilePath, "CarsDB");
                   
                   
                }
            }
            catch(Exception ex)
            {
                throw new Exception(ex.InnerException.Message);
            }
}

i am geting this error msg :
{"Cannot perform a backup or restore operation within a transaction.\r\nBACKUP DATABASE is terminating abnormally."}

whats wrong ?
Tech_MenAsked:
Who is Participating?
 
Tech_MenAuthor Commented:
hi
sorry but your answer doesn't work for me
i figure is up like that :
 ef.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, "EXEC_BACKUP @BACKUPPATH = {0}, @BKPDATABASE = {1}", new object[] { FilePath, "CarsDB" });
0
 
Deepak ChauhanSQL Server DBACommented:
Seems issue here

  string FilePath = "C:\\CarsDB.bak";

change it to

  string FilePath = "C:\CarsDB.bak";
0
 
Deepak ChauhanSQL Server DBACommented:
From the reference of microsoft for the same error. Hope this will help.

You can supperes the transaction scop.

using(TransactionScope ts = new TransactionScope(TransactionScopeOption.Suppress)) {
Your code here....
ts.Complete();
}

https://social.technet.microsoft.com/Forums/es-ES/2c279d4d-217f-4cc1-86c4-8ba704a645eb/cannot-perform-a-backup-or-restore-operation-within-a-transaction-error-when-restoring-database-on?forum=sqlnetfx
0
 
Tech_MenAuthor Commented:
i found how to do it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.