简体   繁体   English

EF ef Database.SqlQuery内部回滚到执行的存储过程

[英]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.

相关问题 EF 6 在使用 Database.SqlQuery 调用存储过程时省略可选参数 - EF 6 omit optional parameters when calling a stored procedure using Database.SqlQuery 来自EF7 Database.SqlQuery的字符串循环列表 - Looping list of strings from EF7 Database.SqlQuery Database.SqlQuery和EF 6.1在性能上有什么区别吗? - Is there any difference in performance between Database.SqlQuery and EF 6.1? 为什么选择EF Database.SqlQuery <T> 不能使用实体类型进行部分选择? - why EF Database.SqlQuery<T> cannot use entity type for partial selection? EF 6.1 Database.SqlQuery投影到复杂类型(多对多关系) - EF 6.1 Database.SqlQuery projection into complex type (many-to-many relationship) 如何使用存储过程通过database.Sqlquery()在数据库中仅找出一条记录 - How to find out only one record in database using stored procedure by database.Sqlquery() 如何使用 DbContext.Database.SqlQuery<TElement> (sql, params) 与存储过程? EF Code First CTP5 - How to use DbContext.Database.SqlQuery<TElement>(sql, params) with stored procedure? EF Code First CTP5 首先使用Database.SqlQuery从Entity Framework Code调用存储过程不起作用 - Calling stored procedure from Entity Framework Code First with Database.SqlQuery is not working Database.SqlQuery线程安全 - Database.SqlQuery thread safety 使用EF的mvc中的存储过程(数据库优先) - Stored procedure in mvc using EF(Database first)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM