0

DbContext といくつかの POCO エンティティで EF を使用しています

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

更新されない quoteDB.ContactPersonAssigned を除いて、quotationDB のすべてのフィールドが更新されます。なんで?実際のオブジェクトは正しく更新されますが、変更はデータベースに保存されません。

4

1 に答える 1

1

ナビゲーション プロパティを仮想として定義する必要があります

 public virtual ContactPerson ContactPersonAssigned { get; set; }
于 2011-09-01T14:16:05.167 に答える