LINQ检索语句未返回最新更新结果

时间:2016-10-11 11:48:30

标签: database vb.net azure linq-to-sql

我有一个名为Organization的表和一个名为Staff的表。

在员工表内,我有两个

字段
  • StaffStatus表示他/她是否可用于该组织
  • ParentStatus用于判断用户的组织是否仍然可用(由于多种原因,我没有链接它们)。

如果员工的ParentStatus为“Available”,我允许所有者恢复员工(将StaffStatus更新为“Available”)。

我的系统中有一种奇怪的情况。我首先删除员工(将StaffStatus更新为“Unavailable”),然后删除组织(将staff.ParentStatus更新为“Unavailable”)。在这种情况下,不允许所有者恢复用户。但是,我检索的ParentStatus是“可用”(因此它仍然允许恢复用户),但在数据库中,ParentStatus已经更改为“不可用”。

我用来删除用户的代码:

Friend Sub removeUser(strInput As String)
    Try
        objUser = (From userDB In db.Staffs Where userDB.UserID = strInput).FirstOrDefault()
        objUser.UserStatus = "Unavailable"
        db.SubmitChanges(ConflictMode.ContinueOnConflict)
    Catch ex As Exception
        For Each occ As ObjectChangeConflict In db.ChangeConflicts
            occ.Resolve(RefreshMode.KeepChanges)
        Next
        db.SubmitChanges()
    End Try        
End Sub

我用来删除组织的代码:

Friend Sub removeOrganization(strInput As String)
    Try
        objOrganization = (From organizationDB In db.Organizations
                           Where organizationDB.OrganizationID = strInput).FirstOrDefault()

        objOrganization.OrganizationStatus = "Unavailable"

        Dim objCompany = From companyDB In db.Companies
                         Where companyDB.OrganizationID = strInput

        For Each mem In objCompany
            mem.ParentStatus = "Unavailable"

            Dim objDepartment = From DepartmentDB In db.Departments
                                Where DepartmentDB.CompanyID = mem.CompanyID

            For Each record In objDepartment
                record.ParentStatus = "Unavailable"
            Next
        Next

        Dim objUser = From UserDB In db.Staffs
                      Where UserDB.OrganizationID = strInput

        For Each mem In objUser
            mem.ParentStatus = "Unavailable"
        Next

        db.SubmitChanges(ConflictMode.ContinueOnConflict)
    Catch ex As Exception
        For Each occ As ObjectChangeConflict In db.ChangeConflicts
            occ.Resolve(RefreshMode.KeepChanges)
        Next
        db.SubmitChanges()
    End Try
End Sub

我如何调用recoverUser(我使用MsgBox()来测试我检索的ParentStatus):

Private Sub btnViewUserRecover_Click(sender As Object, e As EventArgs) Handles btnViewUserRecover.Click
    MsgBox(helperUserCKJ.getUserByID(strUserID).ParentStatus)
    If dgvUser.RowCount > 0 Then
        strUserID = dgvUser.SelectedRows.Item(0).Cells(0).Value
        If (helperUserCKJ.getUserByID(strUserID).ParentStatus.Equals("Unavailable")) Then
            MessageBox.Show("The Organization or Company or Department of the User is unavailable now." & vbNewLine & "You need to recover the Organization or Company or Department of the User first before you can recover the User.", "User Recovery Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            Dim respond = MessageBox.Show("Are you sure want to recover the user?", "User Recovery Comfirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If respond = DialogResult.Yes Then
                helperUserCKJ.recoverUser(strUserID)
            End If
        End If
    End If
End Sub

我用来检索用户的查询:

Friend Function getUserByID(strInput As String) As Staff
    getUserByID = (From userDB In db.Staffs
                   Where userDB.UserID = strInput).FirstOrDefault()
End Function

我可以验证数据库中的ParentStatus字段是否已经更改。

如果我没有删除用户并直接删除该组织,则表示正常(不允许恢复用户)。如果我删除用户然后删除组织,当我单击以恢复用户时,parentStatus是“可用”。

这是一个奇怪的情况。我正在使用LINQ,vb.net和Microsoft Azure数据库。

1 个答案:

答案 0 :(得分:0)

所以我找到了用当前值刷新数据库的解决方案。

Friend Function getUserByID(strInput As String) As Staff
    objUser = (From userDB In db.Staffs
               Where userDB.UserID = strInput).FirstOrDefault()
    db.Refresh(RefreshMode.OverwriteCurrentValues, objUser)
    getUserByID = objUser
End Function