Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在事务下找不到实体_C#_Entity Framework 6 - Fatal编程技术网

C# 在事务下找不到实体

C# 在事务下找不到实体,c#,entity-framework-6,C#,Entity Framework 6,我对这个数据层方法有一个问题 public async Task<(bool HasFailed, IList<string> ErrorMessages)> ApproveBillCancelRequest(IEnumerable<string[]> billsToApprove, string userId, string operationId) { var callerInfo = Shared.Comm

我对这个数据层方法有一个问题

    public async Task<(bool HasFailed, IList<string> ErrorMessages)> ApproveBillCancelRequest(IEnumerable<string[]> billsToApprove,
        string userId, string operationId)
    {
        var callerInfo = Shared.CommonAcross.Helper.GetCaller();
        InfoScope($"{LayerName} -> {callerInfo.MethodName} -> Started", operationId, userId);

        var errorMessages = new List<string>();

        using (var context = new FocusConnection())
        {
            var transaction = context.Database.BeginTransaction();

            try
            {
                foreach (var billToApprove in billsToApprove)
                {
                    var targetBillNumber = billToApprove[0];
                    var targetPayModeId = int.Parse(billToApprove[1]);

                    var entityBill = context.BILL_INFO_CANCEL_REQUESTS
                        .SingleOrDefault(where =>
                            where.BILL_NUMBER == targetBillNumber &&
                            where.PAY_MODE_ID == targetPayModeId);

                    if (entityBill == null)
                    {
                        errorMessages.Add($"Bill #{billToApprove[0]}, payment #{billToApprove[1]} was not found for cancel approval");
                        continue;
                    }

                    entityBill.BILL_INFO.LAST_MODIFIED_BY = userId;
                    entityBill.BILL_INFO.STAMP_DATE = DateTime.Now;
                    entityBill.BILL_INFO.INPUT_STATUS = 0;

                    var cancelledBill = new BILL_INFO_CANCELED
                    {
                        BILL_NUMBER = entityBill.BILL_NUMBER,
                        PAY_MODE_ID = entityBill.PAY_MODE_ID,
                        CASHIER_ID = entityBill.CASHIER_ID,
                        CANCELED_DATE = entityBill.CANCEL_REQUEST_DATE,
                        CANCEL_REQUESTED_BY = entityBill.CANCEL_REQUESTED_BY,
                        CANCEL_APPROVED_BY = userId,
                        REMARKS = entityBill.CANCELATION_REASON
                    };

                    // Add cancelled bill
                    context.BILL_INFO_CANCELEDS.Add(cancelledBill);

                    // Remove cancellation request
                    context.BILL_INFO_CANCEL_REQUESTS.Remove(context.BILL_INFO_CANCEL_REQUESTS.Single(where =>
                        where.BILL_NUMBER == cancelledBill.BILL_NUMBER && where.PAY_MODE_ID == cancelledBill.PAY_MODE_ID));

                    await context.SaveChangesAsync();
                }

                transaction.Commit();
            }
            catch (Exception exp)
            {
                transaction?.Rollback();

                ErrorScope($"{LayerName} -> {callerInfo.MethodName} -> Exception [{exp.Message}]", exp, operationId, userId);
                errorMessages.Add($"{LayerName} -> {callerInfo.MethodName} -> Exception [{exp.Message}]");

                return (true, errorMessages);
            }
        }

        return (errorMessages.Any(), errorMessages);
    }
公共异步任务批准BillCancelRequest(IEnumerable billsToApprove,
字符串用户ID、字符串操作ID)
{
var callerInfo=Shared.CommonCross.Helper.GetCaller();
InfoScope($“{LayerName}->{callerInfo.MethodName}->Started”,操作ID,用户ID);
var errorMessages=新列表();
使用(var context=new FocusConnection())
{
var transaction=context.Database.BeginTransaction();
尝试
{
foreach(var billToApprove in billToApprove)
{
var targetBillNumber=billToApprove[0];
var targetPayModeId=int.Parse(billToApprove[1]);
var entityBill=context.BILL\u INFO\u CANCEL\u请求
.SingleOrDefault(其中=>
其中.BILL_NUMBER==targetBillNumber&&
其中.PAY_MODE_ID==targetPayModeId);
if(entityBill==null)
{
errorMessages.Add($“未找到用于取消批准的账单{billToApprove[0]},付款{billToApprove[1]}”);
继续;
}
entityBill.BILL\u INFO.LAST\u MODIFIED\u BY=userId;
entityBill.BILL\u INFO.STAMP\u DATE=DateTime.Now;
entityBill.BILL\u INFO.INPUT\u STATUS=0;
var CANCELEDBILL=新票据\u信息\u已取消
{
账单编号=实体账单。账单编号,
PAY\u MODE\u ID=entityBill.PAY\u MODE\u ID,
出纳\u ID=entityBill.CASHIER\u ID,
取消日期=实体账单。取消请求日期,
CANCEL\u REQUESTED\u BY=entityBill.CANCEL\u REQUESTED\u BY,
CANCEL\u APPROVED\u BY=userId,
备注=entityBill.Cancellation\u原因
};
//添加已取消的票据
context.BILL\u INFO\u CANCELEDS.Add(canceledbill);
//删除取消请求
context.BILL\u INFO\u CANCEL\u REQUESTS.Remove(context.BILL\u INFO\u CANCEL\u REQUESTS.Single(其中=>
where.BILL_NUMBER==取消的BILL.BILL_NUMBER&&where.PAY_MODE_ID==取消的BILL.PAY_MODE_ID));
wait context.saveChangesSync();
}
Commit();
}
捕获(异常扩展)
{
事务?.Rollback();
ErrorScope($“{LayerName}->{callerInfo.MethodName}->异常[{exp.Message}]”,exp,operationId,userId);
添加($“{LayerName}->{callerInfo.MethodName}->异常[{exp.Message}]”);
返回(true,errorMessages);
}
}
返回(errorMessages.Any(),errorMessages);
}
问题是在ForEach循环中,第一条记录被正确地检索和处理。但未找到剩余记录(
entityBill==null
),但它们位于数据库中。我相信这与正在运行的事务有关


有人能帮忙吗?

这是一个很难解决的问题。结果表明,在UI层上执行String.spliting()时,数组列表中的数组的第一个索引追加了一个额外的空间。因此,我通过以下方式解决了这一问题:

var targetBillNumber = billToApprove[0].Trim();
var targetPayModeId = int.Parse(billToApprove[1].Trim());