我有两个表格:Main
和Vendors
。“无法删除未连接的实体”错误
Main table:
MainID (PK)
Name
Address
...
Vendors table:
VendorID (PK)
MainID (ForeignKey)
Code
....
从Vendors
表中的数据被显示成ListBox
控制。
当我尝试从Vendors
表中删除一行,在列表框控件使用一个按钮,我得到以下错误:
Cannot remove an entity that has not been attached.
删除按钮的代码是:
Dim button = TryCast(sender, Button)
If button IsNot Nothing Then
Using db As New theContext.theContext("Data Source=isostore:/theDB.sdf")
Dim RecordToDelete As Vendors = TryCast(button.DataContext, Vendors)
VendorsRecords.Remove(RecordToDelete)
db.VendorsRecords.DeleteOnSubmit(RecordToDelete)
db.SubmitChanges()
End Using
End If
我得到的错误在行:_db.VendorsRecords.DeleteOnSubmit(RecordToDelete)_ – milo2011