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
}
}
爲什麼表C TC總是空的?
這是僞代碼還是剪切和粘貼?不應該是rac.TableC.First()? – 2013-03-01 00:38:37
它的僞代碼。 – 2013-03-01 00:40:49
你是對的,它一定是。 – 2013-03-01 00:53:03