0

一个奇怪的情况让我震惊,这段代码两天前运行成功,但我不知道为什么它现在没有像以前那样运行:

public static void ChangeStatus(int sessionID, int? participantID, Guid? temporaryParticipantID, int statustypeID)
{
    using (EMSEntities entities = new EMSEntities())
    using (TransactionScope ts = new TransactionScope())
    {
        try
        {
            SessionParticipant sessionParticipant = null;
            CurrentSessionSeatsStatu sessionCurrentStatus = null;
            if (participantID != null)
            {
                sessionParticipant = entities.SessionParticipants
                                             .Where(a => a.SessionID == sessionID && a.ParticipantID == participantID)
                                             .FirstOrDefault();
            }
            else if (temporaryParticipantID != null)
            {
                sessionParticipant = entities.SessionParticipants
                                             .Where(a => a.SessionID == sessionID && a.TemporaryParticipantID == temporaryParticipantID)
                                             .FirstOrDefault();
            }
            if (sessionParticipant != null)
            {
                sessionParticipant.StatusTypeID = statustypeID; // Status Changed here
            }
            **if (sessionParticipant.StatusTypeID == 2) // verified status
            {
                sessionCurrentStatus = entities.CurrentSessionSeatsStatus
                                               .Where(a => a.SessionID == sessionID)
                                               .FirstOrDefault();
                if (sessionCurrentStatus.SeatsLeft > 0)
                {
                    sessionCurrentStatus.SeatsLeft = sessionCurrentStatus.SeatsLeft - 1;
                }
            }**
            entities.SaveChanges();
            ts.Complete();
        }
        catch (Exception ex)
        {
            ts.Dispose();
        }
    }
}

问题是 sessionParticipant 的更改(在 StatusTypeID 中)未保存在数据库中,但 sessionCurrentStatus 更改是!

没有错误抛出什么!

编辑:我发现sessionparticipant除了状态更改为已验证之外,所有情况下都发生了变化。
IE。当另一张桌子即。sessioncurrentstatus在 if 块中更新。
那就是每当它进入这个if block(bold in code)问题时。

最后我发现了问题,我认为这是因为下面的代码但是如果有人能解释确切的原因会很好:

EMS.DAL.DALHelper.AttachAndSaveChanges(sessionParticipant, System.Data.EntityState.Modified); // the position of this code line can be found in the below code

下面是调用 ChangesStatus 方法的代码:

protected void ddlStatuses_SelectedIndexChanged(object sender, EventArgs e)
{
    for (int i = 0; i < gridViewEvents.VisibleRowCount; i++)
    {
        if (gridViewEvents.Selection.IsRowSelected(i))
        {
            EMS.DAL.SessionParticipant sessionParticipant = (EMS.DAL.SessionParticipant)gridViewEvents.GetRow(i);
            EMS.DAL.Session session = EMS.DAL.DALHelper.GetSessionById(sessionParticipant.SessionID);
            EMS.DAL.DALHelper.ChangeStatus(sessionParticipant.SessionID, sessionParticipant.ParticipantID, sessionParticipant.TemporaryParticipantID, Convert.ToInt32(ddlStatuses.SelectedItem.Value));
            if (ddlStatuses.SelectedItem.Value == "2" || ddlStatuses.SelectedItem.Value == "3") // if accepted or rejected
            {
                if (ddlStatuses.SelectedItem.Value == "2")  // verified/accepted
                {
                    EMS.DAL.DALHelper.SendMail("Congratulations! your participation for " + session.Name + " event has been confirmed.", "", sessionParticipant.Email, "");
// AT THIS POINT THE 'sessionParticipant' did not have the changed status which was set in the ChangeStatus method
                    sessionParticipant.IsNotified = true;
                    EMS.DAL.DALHelper.AttachAndSaveChanges(sessionParticipant, System.Data.EntityState.Modified);  // culprit as per me

                    List<EMS.DAL.SessionAttendanceList> attendanceList = EMS.DAL.DALHelper.GetSessionAttendanceList(session.ID);
                    attendanceList.ForEach(a =>
                    {
                        EMS.DAL.AttendanceListDetail attendanceListDetail = a.AttendanceListDetails.Where(p => p.ParticipantID == sessionParticipant.ParticipantID).FirstOrDefault();
                        if (attendanceListDetail == null)
                        {
                            attendanceListDetail.AttendanceListID = a.ID;
                            attendanceListDetail.ParticipantID = sessionParticipant.ParticipantID.Value;
                            attendanceListDetail.IsPresent = false;
                            EMS.DAL.DALHelper.AttachAndSaveChanges(attendanceListDetail, System.Data.EntityState.Added);
                        }
                    });
                }
                else if (ddlStatuses.SelectedItem.Value == "3") // denied/rejected
                {
                    EMS.DAL.DALHelper.SendMail("Your participation for " + session.Name + " event has been denied.", "", sessionParticipant.Email, "");
                    sessionParticipant.IsNotified = true;
                    EMS.DAL.DALHelper.AttachAndSaveChanges(sessionParticipant, System.Data.EntityState.Modified);
                    List<EMS.DAL.SessionAttendanceList> attendanceList = EMS.DAL.DALHelper.GetSessionAttendanceList(session.ID);
                    attendanceList.ForEach(a =>
                    {
                        EMS.DAL.AttendanceListDetail attendanceListDetail = a.AttendanceListDetails.Where(p => p.ParticipantID == sessionParticipant.ParticipantID).FirstOrDefault();
                        if (attendanceListDetail != null)
                        {
                            EMS.DAL.DALHelper.AttachAndSaveChanges(attendanceListDetail, System.Data.EntityState.Deleted);
                        }
                    });
                }
            }
            else
            {
                List<EMS.DAL.SessionAttendanceList> attendanceList = EMS.DAL.DALHelper.GetSessionAttendanceList(session.ID);
                attendanceList.ForEach(a =>
                {
                    EMS.DAL.AttendanceListDetail attendanceListDetail = a.AttendanceListDetails.Where(p => p.ParticipantID == sessionParticipant.ParticipantID).FirstOrDefault();
                    if (attendanceListDetail != null)
                    {
                        EMS.DAL.DALHelper.DeleteAttendanceListDetail(attendanceListDetail);
                    }
                });
                attendanceList.ForEach(a =>
                {
                    EMS.DAL.DALHelper.DeleteSessionAttendanceList(a);
                });
            }
        }
    }
    gridViewEvents.DataBind();
    RefreshSeats();
}
4

0 回答 0