【问题标题】:An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported已尝试附加或添加一个不是新的实体,可能是从另一个 DataContext 加载的。不支持
【发布时间】:2014-05-26 21:00:57
【问题描述】:

我收到此错误,但我不知道在哪里修复。它抛出这个异常

“已尝试附加或添加一个不是新的实体,可能是从另一个 DataContext 加载的。不支持。”

我正在使用委托。添加新数据时会引发事件。

这是活动

Private Sub frmDaiLy_Added(sender As Object, daiLy As DAILY)
    listDaiLy.Add(daiLy) -> it throw exception here
End Sub

这是表单中的函数

Public Sub Add()
    Dim dl As New DAILY With {"data from textbox"}

    Using context As New QLDLDataContext()
        context.DAILies.InsertOnSubmit(dl)
        context.SubmitChanges()
    End Using

    RaiseEvent DaiLyAdd(Me, dl)
End Sub

listDaiLy 类型是 Bingdinglist(Of DAILY)

DAILY 是 linq 创建的类

【问题讨论】:

    标签: linq datacontext notsupportedexception


    【解决方案1】:

    对未显示的代码进行一些假设...列表是在 EF 中具有关系的类的一部分吗?如果是这样,将其添加到列表中然后保存它应该会提供预期的结果。即,将新项目添加到列表中,确保关系,并通过 EF 保存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 2021-10-16
      • 2012-04-28
      相关资源
      最近更新 更多