我有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总是空的?
答案 0 :(得分:1)
你的财产可能有延迟加载的问题。尝试急切加载集合:
db.TableA.Include("RelationAC.TableC")