Entity Framework POCO - Обновите свойство навигации

У меня возникли проблемы с обновлением связанной коллекции объектов.

По сути, проблема заключается в следующем:

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; }
}

Теперь у меня есть графический интерфейс для добавления лекций и , при желании можно отменить процесс редактирования:

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
}

Итак, код плох? Есть ли какой-нибудь метод, который я использую неправильно? Можно ли ПОЛНОСТЬЮ перезагрузить объект целиком? ..

5
задан Şafak Gür 27 August 2013 в 16:37
поделиться