我在刷新相关实体集合方面遇到了一些问题。
问题本质上如下:
public class Student
{
public virtual ICollection<Lecture> Lectures { get; set; }
public void AddLecture(Lecture lecture)
{
Lectures.Add(lecture);
}
public void CancelChanges()
{
_context.Refresh(RefreshMode.StoreWins, this);
_context.LoadProperty(this, (o) => o.Lectures,
MergeOption.OverwriteChanges);
}
}
public class Grade
{
public virtual Student { get; set; }
}
现在我有一些用于添加讲座的GUI,如果需要,我们可以取消编辑过程:
public void ExampleEdit()
{
Student student = _context.Students.SingleOrDefault(/* blah */);
student.AddLecture(_context.Lectures.SingleOrDefault(/* e.g. math */));
student.CancelChanges();
// At this point student SHOULD have no lectures anymore since the
// property was loaded with overwrite changes option.
// Yet the Lectures still contains the lecture we added there
}
那么,这段代码有问题吗?我是否使用了错误的方法?是否可能完全重新加载整个对象?