Link to home
Start Free TrialLog in
Avatar of Tech_Men
Tech_MenFlag for Israel

asked on

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 ?
Avatar of Deepak Chauhan
Deepak Chauhan
Flag of India image

Seems issue here

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

change it to

  string FilePath = "C:\CarsDB.bak";
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
ASKER CERTIFIED SOLUTION
Avatar of Tech_Men
Tech_Men
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tech_Men

ASKER

i found how to do it