2016-09-07 1 views
1

ID, 이름 및 BinaryData 속성이있는 첨부 파일을 포함 할 수있는 펀치가 포함 된 모델이 있습니다.EF6에 부분 엔터티 만 포함하십시오.

내가 할 경우 : 첨부 파일이 많은 대형 모두가 될 수 있기 때문에

var result = context.PunchSet 
    .Where(p => p.PunchType == punchType && p.Project.Id == projectId) 
    .Include(c => c.Contractor) 
    .Include(c => c.ClearedBy) 
    .Include(c => c.CreatedBy) 
    .Include(a => a.Attachments) 

쿼리가 molassis으로 느립니다. 이 경우 필요한 것은 첨부 파일의 ID와 이름입니다. 그래서 시도 :

var result = context.PunchSet 
    .Where(p => p.PunchType == punchType && p.Project.Id == projectId) 
    .Include(c => c.Contractor) 
    .Include(c => c.ClearedBy) 
    .Include(c => c.CreatedBy) 
    .Include(a => a.Attachments.Select(a2 => new Attachment() { Id=a2.Id, Name=a2.Name}); 

을하지만이 오류와 함께 종료 :

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path

무슨 뜻인지 생각을하지 않은 내가 시간 동안 갇혀 있었어요. 어떻게 결과에 부분적인 entitiy를 포함시킬 수 있습니까? 나는. 바이너리 데이터를 읽지 마라.

+0

원하는대로

var result = from p in context.PunchSet where (p.PunchType == punchType && p.Project.Id == projectId) select new { p, Contractor=p.Contractor, ClearedBy =p.ClearedBy, CreatedBy=p.CreatedBy, Attachments= from a in p.Attachments select new { Id= a.Id, Name =a.Name, }, }; var result2 = result.AsEnumerable() .Select(c => c.p); 

그 후 당신은 반복 할 수 있습니다. 그러나 업데이트가 아닌 읽기 만하고 싶다면 첨부 파일을 제외하고 별도로로드 할 수 있습니다. – grek40

+1

같은 쿼리에서 어떻게 할 수 있습니까? 나는 500 개의 펀치를 적재하고 싶지는 않지만 루프가 있어야만 첨부 파일을 가져올 수있다. 좋은 SQL에서 5 분이 걸릴 것입니다. 때로는이 EF6 재료가 나에게 다가 가고 있습니다. – Paaland

+1

부분적으로 관련된 엔티티를로드해야하는 경우 익명 형식 또는 DTO를 사용하여 쿼리를 투영해야합니다. 예외가 설명하는 것처럼'Include' 확장 메소드에서만 네비게이션 프로퍼티를 참조 할 수 있습니다 – octavioccl

답변

1

단일 쿼리에서 원하는 모든 속성을 선택한 다음 메모리에서 함께 결합 할 수 있습니다.

db.PunchSet 
    .Include(x => x.Contractor) 
    // ... other includes of complete objects 
    // then select properties for partial include 
    .Select(x => new { obj = x, att = x.Attachments.Select(a => new { a.Id, a.Name }) }) 
    // end of database query context 
    .AsEnumerable() 
    // join the results in memory 
    .Select(x => 
    { 
     x.obj.Attachments = x.att.Select(a => new Attachment() { Id = a.Id, Name = a.Name }).ToList(); 
     return x.obj; 
    }); 
0

다음과 같이 시도 할 수 있습니다. 당신은 당신이 부분적으로 포함 할 수 없습니다 :

foreach(var r in result2) 
{ 
    foreach(var a in r.Attachments) 
    { 
     //your code; 
    } 
} 
관련 문제