【问题标题】:"Cannot remove an entity that has not been attached" error“无法删除尚未附加的实体”错误
【发布时间】:2012-05-31 23:16:23
【问题描述】:

我有两个表:MainVendors

Main table:
MainID (PK)
Name
Address
...

Vendors table:
VendorID (PK)
MainID (ForeignKey)
Code
....

Vendors 表中的数据显示在ListBox 控件中。

当我尝试使用 ListBox 控件中的按钮从 Vendors 表中删除一行时,我收到以下错误:

无法移除尚未附加的实体。

删除按钮代码为:

        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)

标签: linq-to-sql listbox windows-phone-7.1 delete-row


【解决方案1】:

添加行

db.VendorsRecords.Attach(RecordtoDelete)

在给你一个错误的那一行之前。

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

【讨论】:

  • 我做了,我得到了这个错误:'试图附加或添加一个不是新的实体,可能是从另一个 DataContext 加载的。这不受支持。在新添加的行。
  • 返回新错误:无法使用已在使用的键添加实体。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 2021-03-06
相关资源
最近更新 更多