2012-05-30 14 views
0

我有两个表格:MainVendors“无法删除未连接的实体”错误

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 
+0

我得到的错误在行:_db.VendorsRecords.DeleteOnSubmit(RecordToDelete)_ – milo2011

回答

1

添加行

db.VendorsRecords.Attach(RecordtoDelete) 

之前,行给你一个错误。

您正在收到此错误,因为db上下文不知道您要删除的记录。

+0

我做了,我得到这个错误:'已尝试附加或添加一个非新的实体,可能已被加载来自另一个DataContext。这不被支持。'在新增的行中。 – milo2011

+0

返回新错误:无法使用已在使用的密钥添加实体。 –