在实体框架中调用TransactionScope中的存储过程

时间:2021-09-03 21:48:43

I using Entity Framework 4 and meet following issue whith executing stored procedure in ambient transaction. Here is the code:

我使用实体框架4并在执行环境事务中的存储过程时满足以下问题。这是代码:

public void UpdateOrderRequest(IOrder order, int requestId, int userId, Fee fee)
{
    using (var tscope = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        _storedProcedureDA.UpdateOrderRequest(requestId, userId, data.ClientId, data.RequestStatus, data.Date,
                              data.Type, data.Side, data.Quantity, data.ExecInst, data.Price,
                              data.StopPrice, data.TimeInForce, data.Description, data.Target);
        var feeDa = new FeeDA();
        var dbFee = new Domain.Entities.Fee
                        {
                            OrderRequestId = requestId,
                            Identifier = fee.Id,
                            Value = fee.Value,
                        };
        feeDa.Save(dbFee);
        tscope.Complete();
    }
}
  1. _StoredProceduresDA and FeeDA are data access classes that uses one instance of DataContext for each.
  2. _storedprogramduresda和FeeDA是数据访问类,每个类使用一个DataContext实例。
  3. _storedProcedureDA.UpdateOrderRequest() method is just wrapper under Context.ExecuteFunction<..>("AddOrderRequest",...)
  4. updateorderrequest()方法只是在Context.ExecuteFunction<. >(“AddOrderRequest”,…)下的包装。
  5. feeDA.Save() adds entity into Repository and calls Context.SaveChanges()
  6. save()将实体添加到存储库并调用Context.SaveChanges()
  7. When i trying to make this call, i catching following exception: The transaction operation cannot be performed because there are pending requests working on this transaction.
  8. 当我尝试进行此调用时,我捕获了以下异常:事务操作无法执行,因为在此事务上有等待请求。

The point is that i need to do both of these operations in one transaction and i can't use workaround suggested in Can't I call a stored procedure from Entity Framework inside a transaction scope? (ado.net using its own connection) Does anyone knows how to wrap DataContext.ExecuteFunction<>() in transaction?

关键是我需要在一个事务中同时执行这两个操作,我不能使用在事务范围内的实体框架中调用存储过程吗?(ado.net使用自己的连接)是否有人知道如何在事务中包装DataContext.ExecuteFunction<>() ?

P.S. I've tried to wrap ExecuteFunction in its own transaction with its own TransactionScope with all possible parameters(Supress and so on) but hothing helped.

另外,我尝试过在自己的事务中使用所有可能的参数(Supress等)将ExecuteFunction封装到自己的事务中,但是hothing起了作用。

1 个解决方案

#1


3  

Have a look at the similar issue in this other * question , particularly at answer #4:

看看另一个*问题中的类似问题,尤其是答案4:

"I Finally found a solution...it seems EF expects the stored proc (imported function) to return a value. so call .FirstOrDefault() on the function when it returns."

“我终于找到了解决办法……”EF似乎希望存储的proc(导入函数)返回一个值。因此调用。firstordefault()函数返回时的函数。

If that's applicable to your own problem, then inside of your _storedProcedureDA.UpdateOrderRequest method, where you call Context.ExecuteFunction, grab the return value (probably int) and return it back to the calling method (so change UpdateOrderRequest from void to int or whatever the return value type is).

如果这适用于您自己的问题,那么在_storedprocedure过程中。UpdateOrderRequest方法,在其中调用上下文。ExecuteFunction,获取返回值(可能是int)并将其返回到调用方法(因此将UpdateOrderRequest从void更改为int或任何返回值类型)。

I see this question is pretty old, so maybe you've long ago resolved it and moved on?

我觉得这个问题很老了,也许你很久以前就解决了这个问题,然后继续前进?

#1


3  

Have a look at the similar issue in this other * question , particularly at answer #4:

看看另一个*问题中的类似问题,尤其是答案4:

"I Finally found a solution...it seems EF expects the stored proc (imported function) to return a value. so call .FirstOrDefault() on the function when it returns."

“我终于找到了解决办法……”EF似乎希望存储的proc(导入函数)返回一个值。因此调用。firstordefault()函数返回时的函数。

If that's applicable to your own problem, then inside of your _storedProcedureDA.UpdateOrderRequest method, where you call Context.ExecuteFunction, grab the return value (probably int) and return it back to the calling method (so change UpdateOrderRequest from void to int or whatever the return value type is).

如果这适用于您自己的问题,那么在_storedprocedure过程中。UpdateOrderRequest方法,在其中调用上下文。ExecuteFunction,获取返回值(可能是int)并将其返回到调用方法(因此将UpdateOrderRequest从void更改为int或任何返回值类型)。

I see this question is pretty old, so maybe you've long ago resolved it and moved on?

我觉得这个问题很老了,也许你很久以前就解决了这个问题,然后继续前进?