2010-01-21 2 views
0

, 그것은, 그러나ADO.NET 엔티티 프레임 워크 쿼크

  int charId = int.Parse(Request.Params["charId"]); 
      EveFPT ctx = new EveFPT(); 
      var theCharQuery = from a in ctx.tblChars 
           where a.id == charId 
           select new 
              { 
               Name = a.name, 
               CorpName = a.tblCorps.name, 
               AllianceName = a.tblCorps.tblAlliances.name 
              }; 
      if(theCharQuery.Count() == 1) 
      { 
       var theChar = theCharQuery.First(); 
       lblCharName.Text = theChar.Name; 
       lblCorpName.Text = theChar.CorpName; 
       lblAllianceName.Text = theChar.AllianceName; 
      } 

를 작동 . 누가 무슨 일이 일어나는 지 아십니까?

답변

1

Entity Framework는 하위 개체를 열심히로드하지 않습니다. 로드되었는지 확인한 다음 Load()를 호출해야합니다.

if(!theChar.tblCorps.IsLoaded) 
{ 
    theChar.tblCorps.Load(); 
} 

여기 MSDN에서 주제에 좋은 읽기는 다음과 같습니다

How to: Explicity Load Related Objects (Entity Framework)

1

내가 첫 번째 예제의 투사 발현에 부하를 간절히 그것을 기대하지 않았을지라도 나는 같은 일을 생각했다 어느 한 쪽. 한번 시도해보십시오 :

var charId= int.Parse(Request.Params["charId"]); 
EveFPT ctx = new EveFPT(); 
var theChar = (from a in ctx.tblChars.Include ("tblCorps") 
       where a.id == charId 
       select new 
       { 
        Name = a.name, 
        CorpName = a.tblCorps.name, 
        AllianceName = a.tblCorps.tblAlliances.name 
       }).FirstOrDefault(); 
if(theChar != null) 
{ 
    lblCharName.Text = theChar.Name; 
    lblCorpName.Text = theChar.CorpName; 
    lblAllianceName.Text = theChar.AllianceName; 
}