2016-10-11 88 views
1

我有一個名爲Organization的表和一個名爲Staff的表。LINQ檢索語句沒有返回最新的更新結果

裏面的工作人員表我有2個字段其

  • StaffStatus以表明他/她是否可用於組織和
  • ParentStatus到inidicate用戶的組織是否仍可用或不(我沒有鏈接他們由於幾個原因)。

如果員工的ParentStatus是「Available」,我允許所有者恢復員工(將StaffStatus更新爲「可用」)。

我的系統出現了一個奇怪的情況。我首先刪除員工(將StaffStatus更新爲「不可用」),然後刪除組織(將staff.ParentStatus更新爲「不可用」)。在這種情況下,所有者不允許恢復用戶。但是,我檢索到的ParentStatus是「Available」(所以仍然允許恢復用戶),但是在數據庫中,ParentStatus已經更改爲「Unavailable」。

我曾經removeUser代碼:

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 

我曾經removeOrganization代碼:

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數據庫。

回答

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