我有一個「活動」實體,這個實體可以有幾個「任務」。流利Nhibernate:一對多的問題
此處可用於實體的代碼:
public class Activity : Entity<int>
{
public virtual int Id { get; set; }
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public virtual Iesi.Collections.Generic.ISet<Task> Tasks { get; set; }
public Activity()
{
Tasks = new Iesi.Collections.Generic.HashedSet<Task>();
}
}
public class Task
{
public virtual int Id { get; set; }
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public virtual Activity Activity { get; set; }
}
映射:
public ActivityMap()
{
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Code);
Map(x => x.Name);
HasMany(x => x.Tasks)
.KeyColumns.Add("Activity")
.AsSet()
.Inverse()
.Cascade.AllDeleteOrphan();
}
public TaskMap()
{
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Code);
Map(x => x.Name);
}
測試:
activity = new Activity
{
Code = "...",
Name = "..."
};
Task task = new Task
{
Code = "...",
Name ="...",
Activity = activity
};
session.Save(activity);
我看到惠普NHProf 2插入。 提交後,我做了一個實體「Activity」的GetById,但「Tasks」屬性是空的。在數據庫中,「活動」字段(實體「任務」)爲空。
嘿克里斯,問題解決了嗎? – gdoron