2011-09-06 3 views
0

방금 ​​EF 4.1 코드 우선을 사용하도록 EF 4.0 솔루션을 전송했습니다. 모든 것이 원활하게 진행되어 DB에서 데이터를 가져 와서 WCF RIA Services 1.0SP2-RC를 사용하여 Silverlight 프로젝트에 몇 시간 만에 표시 할 수있었습니다.RIA 서비스 + 엔티티 프레임 워크에서 연결로드 코드 우선

유일한 문제는 내 도메인 서비스에서 참조를로드하려고 할 때 발생합니다. 내 참조 속성에 [포함] 특성을 추가했습니다. 또한, 나는 여전히 다음과 같은 방법은 아무 것도 반환하지 않습니다 내 DbContext.But에 대한 게으른로드를 활성화합니다 (4.1에 Ef는 4.0에 근무하지만 다음) :

public IEnumerable<Bar> GetBarsInFoo (int fooId, bool direction) 
{ 
    var foo = this.DbContext.Foos.FirstOrDefault(f => f.FooId == fooId); 
    var Bars = foo.Bars.Where(b => b.Direction == direction).ToList(); 
    return Bars; 
} 

그래서 내가 확인하고 참조 수집 있는지 확인해야 가 포함되어 있지 않으면로드하십시오 :

public IEnumerable<Bar> GetBarsInFoo (int fooId, bool direction) 
{ 
    var foo = this.DbContext.Foos.FirstOrDefault(f => f.FooId == fooId); 

    // Additional code to load the relation. 
    if (!this.context.Entry(foo).Collection(f => f.Bars).IsLoaded) 
    { 
     this.context.Entry(foo).Collection(f => f.bars).Load(); 
    } 

    var Bars = foo.Bars.Where(b => b.Direction == direction).ToList(); 
    return Bars; 
} 

위의 코드는 결과를 올바르게 반환합니다.

내가 언급 한 것처럼이 문제를 해결하려면 코드의 많은 부분을 변경해야한다는 것이 문제입니다.

public partial class Foo 
{ 
    [Key] 
    public int FooId {get; set;} 

    [Include] 
    [Association("Foo_Bar", "FooId", "FooId")] 
    [Display(AutoGenerateField = false)] 
    public virtual ICollection<Bar> Bars { get; set; } 
} 

public partial class Bar 
{ 
    [Key] 
    public int BarId {get; set;} 

    [Display(AutoGenerateField = false)] 
    public int FooId { get; set; } 

    [Include] 
    [Display(AutoGenerateField = false, Name = "In Foo", Order = 12)] 
    [Association("Foo_Bar", "FooId", "FooId", IsForeignKey = true)] 
    [ForeignKey("FooId")] 
    public virtual Foo Foo { get; set; } 
} 

내가 EF 핸들이 나에게 확인하지 않고 관계를로드 할 수있는 방법이 있나요 :

여기 내 개체 정의입니까?

답변

0

지연로드없이 살 수 있다고 가정 할 때 하위 개체를 포함하는 명시적인 방법은 Include<T>(...) 확장 방법을 사용하는 것입니다. 귀하의 예에서

public IEnumerable<Bar> GetBarsInFoo (int fooId, bool direction) 
{ 
    var foo = this.DbContext.Foos 
          .Include(f => f.Bars) 
          .FirstOrDefault(f => f.FooId == fooId); 

    var Bars = foo.Bars.Where(b => b.Direction == direction).ToList(); 
    return Bars; 
} 

이것은 모든 하위 개체를로드하는 가장 간단한 방법입니다. 추가 보너스로 두 번이 아니라 데이터베이스로 한 번에 이동합니다.

더 많은 관련 정보는 the ADO.NET team blog에서 찾을 수 있습니다 (CTP5의 경우에도 마찬가지 임).

관련 문제