2016-06-09 3 views
0

나는 EF4에서 쿼리를 사용하여 데이터를 기반으로 다양한 다른 방법 (EF 아님)을 통해 레코드 및 프로세스 정보를 가져 오므로 목록에서 EF 개체를 자주 분리합니다.엔티티 프레임 워크 4 관련 엔티티가로드되지 않음

이 경우, .Include ("...") 메서드를 사용하고 있어도 EntityFramework 4.0에서 관련 엔터티를로드하지 않는 쿼리가 있습니다.

using (MyDBEntities ctx = new MyDBEntities()) 
{ 
    ctx.ContextOptions.LazyLoadingEnabled = false; 

    // Get the first X records that need to be processed 
    var q = (from t in ctx.DBTables 
       .Include("Customer") 
      let c = t.Customer 
      where t.statusID == (int)Enums.Status.PostProcessing 
      && c.isActive == true 
      select t 
      ).Take(batchSize).ToList(); 

    foreach (DBTable t in q) 
    { 
     // this results in c == null 
     Customer c = t.Customer; 

     // However t.CustomerID has a value, thus I know 
     // that t links to a real Customer record 
     Console.WriteLine(t.CustomerID); 
    } 
} 

명시 적으로 명시하고 있지만 고객이 왜로드하지 않는지 이해하는 사람은 누구입니까?

+0

'batchSize'란 무엇입니까? 'foreach' 루프를 사용하여 단계를 밟을 때 얼마나 많은 레코드가 있는지 알 수 있으며 어떤 값을 볼 수 있습니까? –

+0

batchSize는 훨씬 이전에 함수에 전달되는 int입니다. – Jerry

답변

0

문제점의 근본 원인을 찾았습니다. 악마는 "let"명령에 있습니다. 내가 let 또는 두 번째 "from"절 (조인과 같은)을 가질 때마다 ".Includes"는 무시됩니다 !!!

// -- THIS FAILS TO RETRIEVE CUSTOMER 
// Get the first X records that need to be processed 
var q = (from t in ctx.DBTables 
      .Include("Customer") 
     // Using a "let" like this or 
     let c = t.Customer 
     // a "from" like this immediately causes my include to be ignored. 
     from ca in c.CustomerAddresses 
     where t.statusID == (int)Enums.Status.PostProcessing 
     && c.isActive == true 
     && ca.ValidAddress == true 
     select t 
     ).Take(batchSize).ToList(); 

그러나, 나는 하나의 호출에 가져 오기 위해 필요한 ID 년대를 얻을 갈 수 있습니다, 다음 두 번째 통화 "내 포함을 가서"이 있고, 모든 것이 잘 작동합니다.

// Get the first X record IDs that need to be processed 
var q = (from t in ctx.DBTables 
     let c = t.Customer 
     from ca in c.CustomerAddresses 
     where t.statusID == (int)Enums.Status.PostProcessing 
     && c.isActive == true 
     && ca.ValidAddress == true 
     select t.TableID 
     ).Take(batchSize).ToList(); 

// Now... go "deep-load" the records I need by ID 
var ret = (from t in ctx.DBTables 
      .Include("Customer") 
      where q.Contains(t.TableID) 
      select t); 
관련 문제