0

我有 3 张桌子

=================    =================    =================
| TableA        |    | RelationAC    |    | TableC        |
=================    =================    =================
| IdA           |    | Id            |    | IdC           |
|               |    | IdA           |    |               |
| Field1A       |    | IdC           |    | Field1C       |
=================    =================    =================

这是我模型的代码:

public partial class TableA
{
    public TableA()
    {
       this.RelationAC = new HashSet<RelationAC>();
    }

    [Key]
    public decimal IdA { get; set; }
    public string Field1A { get; set; }
    [ForeignKey("IdA")]
    public virtual ICollection<RelationAC> RelationAC { get; set; }
}

public partial class TableC
{
    [Key]
    public decimal IdC { get; set; }
    public string Field1C { get; set; }
}


public partial class RelationAC    
{
    public RelationAC    ()
    {
        this.TableC= new HashSet<TableC>();
    }
    [Key]
    public decimal Id { get; set; }
    public decimal IdA{ get; set; }
    public decimal IdC{ get; set; }

    [ForeignKey("IdC")]
    public virtual ICollection<TableC> TableC { get; set; }
}

如果进行此查询

var query = from d in db.TableA 
            select d;


foreach( TableA  ta in query.Tolist())
{
    foreach(RelationAC rac in ta.RelationAC.Tolist())
    {
        TableC tc = rac.TableC.First(); // It allways has count = 0 , even my db has data
    } 
}

为什么 TableC tc 总是空的?

4

1 回答 1

1

您的财产可能存在延迟加载问题。尝试急切加载集合:

db.TableA.Include("RelationAC.TableC")
于 2013-03-01T03:26:29.117 回答