我在刷新相关的实体集合时遇到了一些麻烦。
本质上,问题如下:
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
}
那么,代码不好吗?有什么方法我使用不正确吗?是否可以完全重新加载整个对象?..