2011-02-09 3 views
1

내 응용 프로그램에서 두 가지가 동시에 발생합니다. 타이머는 백그라운드 스레드에서 몇 초마다 데이터 표를 업데이트하라는 요청을 시작합니다.Entity Framework - 동시에 여러 쿼리를 실행할 때의 오류

/// <summary> 
/// Refreshes the specified location data grid 
/// </summary> 
/// <param name="sender">Instance of GridLookupEdit to update</param> 
private static void RefreshLocations(object sender) { 

    GridLookUpEdit objEditor = sender as GridLookUpEdit; 

    objEditor.Properties.DataSource = BRData.Models.LocationModel.GetLocationList(true); 
    objEditor.Properties.DisplayMember = "locationName"; 
    objEditor.Properties.ValueMember = "locationID"; 

} 

난 문제 :

// Query 
    var qryPickupRequests = from pr in objDataContext.pickupRequests 
           .Include("toLocation") 
           .Include("fromLocation") 
           .Include("person") 
          orderby pr.creationDate ascending 
          select pr; 

    // Refresh from server? 
    if (boolRefreshFromServer) 
     (qryPickupRequests as ObjectQuery).MergeOption = MergeOption.PreserveChanges; 

    // Limit? 
    if (intLimit > 0) 
     return qryPickupRequests.Take(intLimit).ToList<pickupRequest>(); 
    else 
     return qryPickupRequests.ToList<pickupRequest>(); 

UI 스레드에서, 또 다른 형태는 그 지역의 그리드를 업데이트하고 열이, 이제 다음은 그 쓰레드에 의해 실행되는 코드입니다 이 두 코드 블록이 정확히 같은 시간에 실행되는 것입니다.

enter image description here

내부 예외는 다음과 같다 :

There is already an open DataReader associated with this Connection which must be closed first.

동시 데이터베이스 작업이 엔티티 프레임 워크에 의해 제대로 처리되지 왜 어떤 아이디어 ---- 또는 내 옆에 나는 다음과 같은 오류가 발생합니다 ?

답변

4

엔티티 프레임 워크 개체 컨텍스트는 스레드로부터 안전하지 않습니다. 사용하는 각 스레드마다 다른 컨텍스트를 사용해야합니다. 예제 코드에서는 명확하지 않지만 문제가있는 곳을 처음 추측 할 수 있습니다.

+0

네 말이 맞아. 나는 그들이 쓰레드에 안전하지 않다는 것을 몰랐다. 감사! –

관련 문제