我有一個SQLite數據庫設置,所以當我刪除一個人刪除級聯。當我手動刪除一個人(刪除引用PersonID的所有記錄)時,這工作正常。但是當我使用實體框架刪除的人,我得到一個錯誤:級聯刪除使用實體框架和System.Data.SQLite的問題
System.InvalidOperationException: 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.
我不明白這是爲什麼發生。我的觸發器設置爲清除所有相關對象,然後刪除它被告知要刪除的對象。
當我進入模型編輯器並檢查關係的屬性時,它不顯示OnDelete屬性的操作。爲什麼從數據庫中拉出它不正確?如果我將此值更改爲級聯,則一切正常,但我寧願不要依賴此手動更改,因爲如果從數據庫刷新我的模型並且它丟失了該怎麼辦。
下面是我的表的relivent SQL。
CREATE TABLE [SomeTable]
(
[SomeTableID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[PersonID] INTEGER NOT NULL REFERENCES [Person](PersonID) ON DELETE CASCADE
)
CREATE TABLE [Person]
(
[PersonID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
)
有沒有辦法解決你是否應該更新End1或End2?或者它只是試驗和錯誤? – 2011-10-28 10:55:11
這取決於你的數據庫建模的方式。如果你有一對多的關係,'一個'端應該被設置爲'Cascade' – Omar 2011-10-28 14:08:28