Rana Ali Rana Ali - 2 months ago 27
SQL Question

when i call store procedure from C# it through exception.

CREATE PROCEDURE GetDatBaseBackup

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 store procedure. when i execute this store procedure it works fine.

When i call with the object of data base in my c sharp code it through exception.

public bool DataBaseBackup()
{

int obj= _dbContext.GetDatBaseBackup();
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.\r\nBACKUP 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");