[英]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). 我使用EF 6 Database.SqlQuery语句执行存储过程,实现错误和事务处理,具有打开和关闭事务(处理多个记录,如果只有一个记录回滚有错误,并提交任何其他完成而没有错误)。
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. EF使用自己的事务来执行Database.SqlQuery,但是当发生错误时,我回滚到存储过程中,这个回滚也适用于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. System.Data.SqlClient.SqlException(0x80131904):无法提交当前事务,也无法支持写入日志文件的操作。 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? 在这种情况下,如何防止EF使用自己的交易,或者阻止程序关闭EF的交易?
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). 我发现在这种情况下,没有添加.SqlQuery命令的附加事务,但是错误是由执行过程产生到初始执行的存储过程中(有点复杂,但它确实是必需的)。 However I had this problem with external EntityFramework transaction with .ObjectContext.ExecuteStoreCommand but I resolved this using TransactionalBehavior.DoNotEnsureTransaction behaviour. 但是我使用.ObjectContext.ExecuteStoreCommand与外部EntityFramework事务有这个问题,但我使用TransactionalBehavior.DoNotEnsureTransaction行为解决了这个问题。
For Example: 例如:
using (INotificationDataContext context = DataContextFactory.Create<INotificationDataContext>())
{
result = (context as IObjectContextAdapter).ObjectContext.ExecuteStoreCommand(TransactionalBehavior.DoNotEnsureTransaction, sql, parameters);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.