我有一个带有 Entity Framework 5 代码的应用程序,这是我的类的示例:
public partial class PlaningCourseAssistant
{
public PlaningCourseAssistant() {}
public int ID { get; set; }
public int PlaningCourseID { get; set; }
public int AssistantID { get; set; }
public virtual PlaningCourse PlaningCourse { get; set; }
public virtual Teacher Assistant { get; set; }
}
和
public class Teacher
{
public Teacher()
{
...
this.PlaningCourseAssistants = new HashSet<PlaningCourseAssistant>();
}
public int ID { get; set; }
public string Name{ get; set; }
....
public virtual ICollection<PlaningCourseAssistant> PlaningCourseAssistants { get; set; }
}
和
public partial class PlaningCourse
{
public PlaningCourse()
{
...
this.PlaningCourseAssistants = new HashSet<PlaningCourseAssistant>();
}
public int ID { get; set; }
public string Title { get; set; }
...
public virtual ICollection<PlaningCourseAssistant> PlaningCourseAssistants { get; set; }
}
当我PlaningCourseAssistant
像这样向我添加一个新对象时:
var pcb = new PlaningCourseAssistant
{
PlaningCourseID = CourseID,
AssistantID = assistantID,
};
try
{
planingCourseAssistantService.Add(pcb);
uow.SaveChanges();
} catch { }
之后当我想获取PlaningCourseAssistant
列表时,添加PlaningCourseAssistant
的Assistant
导航键为空。
为什么会这样?我该如何解决这个问题?