請考慮以下關係:級聯刪除 - 映射到表的複雜類型
Car(entity)1 ---------------------- > *輪子(實體)
輪(實體)1 ---------> 1螺母(複雜類型)
輪(實體)1 ---------> 1 Rim(複雜類型)
Nut和Rim複雜類型都映射到名爲Nuts和Rims的表。我使用Wheel ID作爲Nuts和Rims主鍵。
現在,嘗試使用代碼來刪除車的時候,我得到以下異常:
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.
at System.Data.Entity.Core.Objects.ObjectContext.PrepareToSaveChanges(SaveOptions options)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
當試圖做到這一點在MS-SSMS,我得到這個錯誤: DELETE語句衝突與參考約束「FK_dbo.Nuts_dbo.Wheel_Id」。衝突發生在數據庫「DatabaseName」,表「dbo.Nuts」,列'Id'中。
鑑於,複雜類型是實體的必需參數,並且具有一對一的關係,爲什麼在這種情況下,級聯刪除默認爲ON? 二,如何去刪除CAR及其相關的多個Wheels以及所有關聯的Nuts和Rims。最後,就我而言,一輛車有數千個車輪。這是一個好主意,在代碼或使用存儲過程?
謝謝。
如果堅果和輪圈是複雜的類型,那麼它們將被映射到輪盤表中的列。如果他們有自己的表,那麼他們是具有1對1關係的實體。 – 2014-10-17 15:48:48