Обновление связанных сущностей Entity Framework DbContext

Я использую EF с DbContext и несколькими сущностями POCO.

     public class Customer
    {
        public int ID { get; set; }

        public string CompanyName { get; set; }

        public string Address { get; set; }

        public string Email { get; set; }

        public string Phone { get; set; }

        public List<Project> Projects { get; set; }

        public List<ContactPerson> ContactPersons { get; set; }
}

    public class ContactPerson
        {
            public int ID { get; set; }

            public string Name { get; set; }

            public string Email { get; set; }

            public string Phone { get; set; }
}



public class Quotation
    {
        public int ID { get; set; }

        public string QuotationName { get; set; }

        public DateTime DateCreated { get; set; }

        public ContactPerson ContactPersonAssigned { get; set; }

        public string OurReference { get; set; }

        public string QuotationDataString { get; set; }
}

Теперь мне нужно обновить цитату

        using (var myDB = new MyDB())
        {   
            Quotation quotationDB = myDB.Quotations.Find(this.quotation.ID);
            quotationDB.QuotationName = textBox_name.Text;
            quotationDB.OurReference = textBox_quotedBy.Text;
            quotationDB.ContactPersonAssigned = null;
            quotationDB.ContactPersonAssigned = myDB.ContactPersons.Find(((ContactPerson)this.comboBox_YourReference.SelectedItem).ID);
            quotationDB.DateCreated = this.dateTimePicker_dateQuoted.Value;

 myDB.SaveChanges();
                }

Все поля в quotationDB будут обновляться, за исключением quotationDB.ContactPersonAssigned, которое никогда не будет обновляться. Почему? Фактический объект обновляется правильно, но никакие изменения не сохраняются в базе данных.


person Francesco    schedule 01.09.2011    source источник


Ответы (1)


вам нужно определить свойства навигации как виртуальные

 public virtual ContactPerson ContactPersonAssigned { get; set; }
person Jayantha Lal Sirisena    schedule 01.09.2011
comment
Я пробовал это без результатов. В любом случае, почему он должен быть виртуальным? - person Francesco; 01.09.2011
comment
Фиксированный. Я делал еще одну ошибку - person Francesco; 02.09.2011
comment
Отлично. Тогда обновите свой пост с другой ошибкой. Это будет помощь другим. - person Jayantha Lal Sirisena; 02.09.2011