事务没有在实体框架中提交 [英] Transaction is not getting commit in entity framework

查看:198
本文介绍了事务没有在实体框架中提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用实体框架执行复杂过程并在事务表中保存数据。当我的代码提交事务然后它抛出异常



由于有待处理的请求处理此事务,因此无法执行事务操作。



以下是代码。



Trying to execute complex procedure and saving a data in table in transaction using entity framework. When my code commits the transaction then it is throwing exception

The transaction operation cannot be performed because there are pending requests working on this transaction.

Below is the code.

using (abcEntities dbContext = new abcEntities())
        {
            using (var transaction = dbContext.Database.BeginTransaction())
            {
                dbContext.USP_ResellerCustomerFile_DeleteFiles(customerId, fileId, 1);
                ResellerAndCustomerActivityDAL objCustomerActivityDAL = new ResellerAndCustomerActivityDAL();

                ResellerAndCustomerActivity objActivity = new ResellerAndCustomerActivity()
                {
                    Activity = objfile.FileName + " is temporairly permanently deleted.",//message.ReplacingSpecialCharacterswithEntities(),
                    ActivityDate = DateTime.UtcNow,
                    ResellerAndCustomerId = customerId,
                    UserTypeId = 2

                };

                dbContext.ResellerAndCustomerActivities.Add(objActivity);

                //objCustomerActivityDAL.Create(objfile.FileName + " is permanently deleted.", objUnit, out returnMessage, customerId);

                dbContext.SaveChanges();
                transaction.Commit();
            }
        }



不知道为什么它会抛出待处理的请求异常,因为我在同一个交易中执行它。



我尝试了什么:



我试图在谷歌上找到它但没有成功。


Don't know why it is throwing pending request exception because I am executing it in same transaction.

What I have tried:

I have tried to find it on google but no success.

推荐答案

这可能是因为Entity Framework默认会自动包装事务中的所有更新,插入和删除操作。您不必创建自己的交易上下文。



了解更多信息在其中 [ ^ ]。
That would probably be because Entity Framework automatically wraps all update, insert and delete operations in a transaction by default. You don't have to create your own transaction context.

Read more on it here[^].


我找到了另一种方法。



而不是直接从dbcontext调用存储过程我我叫它像dbContext.Database.DbRawSqlQuery。现在我按照以下顺序执行上述所有操作



1)声明并初始化dbcontext。

2)创建事务。

3)使用在步骤1中初始化的上下文执行过程。

4)使用相同的ddContex保存数据库中的记录。

5)保存从dbContext到数据库的更改。

6)提交交易。



甚至不需要创建事务,因为所有实体(与数据库相关)语句都是在默认情况下交易。可以在
I found an alternate way.

Rather then calling Stored Procedure from dbcontext directly I am calling it like dbContext.Database.DbRawSqlQuery. Now I am doing all the above thing in following order

1) Declaring and initializing dbcontext.
2) Creating transaction.
3) Executing procedure using context initialized in step 1.
4) Saving record in databae using same ddContex.
5) saving changes from dbContext to database.
6) Committing transaction.

Even transaction is not needed to create because all the entity(related to database) statements are executed in transaction by default. It can be read
here




这篇关于事务没有在实体框架中提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆