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?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With