2016-07-04 3 views
1

내가 몇 엔티티 프레임 워크 7 (코어) 실체가 람다 표현식 사전을 기반으로 :은 다음과 같습니다 적용하고 ThenInclude

public class Person { 
    public virtual Address Address { get; set; } 
    public virtual ICollection<Hobby> Hobbies { get; set; } 
} 

public class Address { 
    public String Street { get; set; } 
    public virtual Country Country { get; set; } 
} 

을 다음과 같이 내가 문자열 배열이 있습니다

String[] entities = new String[] { "Hobbies", "Address.Country" } 

이 문자열을 감안할 때 배열 내가 가지고 :

,369 : 내가 좋아하는 뭔가를 할 수 EF6에서

context.Persons 
    .Include(x => x.Hobbies) 
    .Include(x => x.Address).ThenInclude(x => x.Country); 

context.Persons.Include(entities[0]).Include(entities[1]); 

그러나 EF7에서 포함은 문자열을 허용하지 않습니다.

private readonly Dictionary<String, LambdaExpression> _properties = new Dictionary<String, LambdaExpression>(); 

같은 것이다 : 나는 사전 생성

x => x.Hobbies is for "Hobbies" 
x => x.Address.Country is for "Address.Country"  

을 그리고 확장 있습니다

public static IQueryable<T> Include<T>(this IQueryable<T> source, Dictionary<String, LambdaExpression> properties) { 
} 

나는 사전은 다음을 적용 제공해야합니다

  1. 포 R "X => x.Hobbies"단지 수행

    source.Include(x => x.Address).ThenInclude(x => x.Country); 
    

수있는이 : 표현식이 "X => x.Address.Country"추가 같은이

source.Include(x => x.Hobbies); 
  • 경우 끝내라?

  • 답변

    0
    ThenInclude() 및 EF 7 확실

    하지,하지만 당신은 당신의 저장소에서이 같은 (EF 6 테스트) 수행 할 수 있습니다

    public MyEntity GetMyEntity_EagerlyLoad(DbContext context, int id, params Expression<Func<MyEntity, object>>[] propertiesToLoad) 
    { 
        var q = context.MyEntities.Where(m => m.ID == id); 
    
        foreach (var prop in propertiesToLoad) 
         q = q.Include(prop); 
    
        return q.SingleOrDefault(); 
    } 
    

    당신은 다음과 같이 호출 할 수 있습니다

    repo.GetMyEntity_EagerlyLoad(context, id, m => m.Property1, m => m.Property2, m => m.Property1.NestedProperty) 
    


    EDIT : 프로젝션을 사용하여 EF로 열망하는 방법도 있습니다.

    public MyEntity GetMyEntity_EagerlyLoad<T>(DbContext context, int id, Expression<Func<MyEntity, T>> loadingProjection, Func<T, MyEntity> resultProjection) 
    { 
        var q = context.MyEntities.Where(m => m.ID == id); 
    
        return q.Select(loadingProjection).AsEnumerable().Select(resultProjection).SingleOrDefault(); 
    } 
    

    을 그리고 당신은로드 할 속성과 메소드 반환 할 엔티티로 전화 : 당신은이 같은 일반적인 저장소 방법을 할 수 EF7 아이 엔티티에서

    repo.GetMyEntity_EagerlyLoad(context, id, m => new { myEntity = m, m.Property1, m.Property2, m.Property1.NestedProperty }, m => m.myEntity) 
    
    +2

    를 포함해야 ThenInclude를 사용하여 ... 그건 내 문제 중 하나입니다 ... –

    +0

    나는 아직 그것을 사용하지 않았기 때문에 다시 EF 7에 대한 확신이 없지만 어쩌면 이것이 당신을 도울 것입니다. –

    관련 문제