2012-07-20 43 views
0

我有以下查询通过包容性的日期,得到的结果:LINQ查询:获取的所有领域,包括外键

var q = from x in db.TableX 
        where x.Timestamp.CompareTo(fromDate) >= 0 
         && x.Timestamp.CompareTo(toDate) <= 0 
        select x; 

我的TableX有一个以上的外键。然而,当我调试时,我只看到其中一个提取,而其他所有的键都是空的,即使我在数据库中看到它们不是空的,并且ID与它们的外部表连接。

public class TableX 
    {   
     public int Id { get; set; } 
     public string str1{ get; set; } 
     public Table2 t1{ get; set; } 
     public Table3 t2{ get; set; } 
     public Table4 t3{ get; set; } 
     public Table5 t4{ get; set; } 
     public Tablet5 t5{ get; set; } 
     } 
+0

你可以发布你的TableX类吗? – 2012-07-20 03:30:54

+0

@Jayantha:我刚刚做到了。 – 2012-07-20 03:35:45

回答

1

Sinse您使用延迟加载,你需要定义导航属性为虚拟的,你需要启用代理的创建,使EF可以围绕你的类的代理,并覆盖这些属性在需要时加载。

public class TableX 
    {   
     public int Id { get; set; } 
     public string str1{ get; set; } 
     public virtual Table2 t1{ get; set; } 
     public virtual Table3 t2{ get; set; } 
     public virtual Table4 t3{ get; set; } 
     public virtual Table5 t4{ get; set; } 
     public virtual Tablet5 t5{ get; set; } 
    } 
+0

懒加载默认?我该如何改变它? – 2012-07-20 03:42:40

+0

谢谢你,这工作。 – 2012-07-20 03:45:26

+0

'db.Configuration.LazyLoadingEnabled = true;'好极了! – 2012-07-20 03:46:49