Rana Ali Rana Ali - 2 months ago 12
SQL Question

When I call a stored procedure from C# it throws an exception

CREATE PROCEDURE GetDatabaseBackup
AS
BEGIN
DECLARE @MyFileName varchar(1000)

SELECT
@MyFileName= 'D:\DATABASE-BACKUP\genxMultiERPLive -' +
REPLACE(convert(nvarchar(20),GetDate(),120),':','-') + '.bak'

BACKUP DATABASE genxMultiERPLive
TO DISK = @MyFileName
END
GO


This is my stored procedure. When I execute this stored procedure, it works fine. When I call with the object of database in my C# code, it throws an exception.

public bool DataBaseBackup()
{
int obj = _dbContext.GetDatabaseBackup();

if (obj != 0)
{
return true;
}

return false;
}


_dbContext
is object of my data base.

Exception is :


Cannot perform a backup or restore operation within a transaction.

BACKUP DATABASE is terminating abnormally.

Answer

You should change the line that call the stored procedure from

int obj= _dbContext.GetDatBaseBackup(); To

_dbContext.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction,"GetDatBaseBackup");

Comments