2011-03-11 117 views
1

我一直在努力解決這個問題幾天了,我仍然無法弄清楚。我已經創建了一個示例項目,希望能幫助解決這個問題。主要問題是,當我從我的上下文加載用戶並在此對象上執行UpdateModel()時,似乎刪除了我的實體引用,並在子對象中獲得空引用。CTP 5 EF代碼第一次UpdateModel()

以下是錯誤:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted

這裏是鏈接到代碼: Here(第42行,導致發生錯誤)

+0

請勿使用UpdateModel – Omu 2011-03-11 19:06:43

+0

當我更新子對象集合時,您會有什麼建議? – 2011-03-11 19:13:51

+0

先做手動,之後你可能會尋找一種方法使其具有通用性 – Omu 2011-03-11 21:44:00

回答

2

感謝Morteza Manavi在實體框架網站上提出這個問題。我的問題是由我的ContactInformation模型屬性引起的,'contactid'&'contacttypeid'不可爲空。一旦我解決這個問題,UpdateModel()就可以正常工作。非常感謝你!

0

你有沒有使用像[必需]或[StringLength]您的鍵值任何數據註解,這可以解釋錯誤消息。

+0

不,現在還沒有。我已經刪除了包括密鑰在內的所有數據註釋,但仍然出現此錯誤。我更新了谷歌代碼網站以反映這一點:http://code.google.com/p/contactsctp5/source/browse/Contacts/Contacts/Controllers/ContactsController.cs – 2011-03-12 19:37:06