Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF ef Database.SqlQuery internal rollback into executed stored procedure

I am using EF 6 Database.SqlQuery statement to execute stored procedure with implemented error and transaction handling, with opening and closing transactions (processing more than one record, if there is error with one record rollback only this, and commit anything else completed without errors).

List<T> _result = this.Database.SqlQuery<T>(sqlStatement, parameters).ToList();

EF uses its own transaction for execution of Database.SqlQuery, but when error occures, and I do rollback into stored procedure this rollback also applies on EF transacton. So i get the follwoing exception:

System.Data.SqlClient.SqlException (0x80131904): The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction.

How can i prevent EF from using his own transaction in this case, or prevent procedure from closing EF's transaction?

like image 650
Igor N Avatar asked Nov 11 '22 02:11

Igor N


1 Answers

I found out in this case there wasn't additonal transaction added by .SqlQuery command, but the error was produced by executed procedure into the inital executed stored procedure (yeh little bit complicated but it is really required). However I had this problem with external EntityFramework transaction with .ObjectContext.ExecuteStoreCommand but I resolved this using TransactionalBehavior.DoNotEnsureTransaction behaviour.

For Example:

        using (INotificationDataContext context = DataContextFactory.Create<INotificationDataContext>())
        {
            result = (context as IObjectContextAdapter).ObjectContext.ExecuteStoreCommand(TransactionalBehavior.DoNotEnsureTransaction, sql, parameters);
        }
like image 98
Igor N Avatar answered Nov 14 '22 21:11

Igor N