I get this error but i dont know where to fix. It throw this exception

"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."

I'm using delegate. It raises event when add new data.

This is the event

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

Here's the function in form

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 type is Bingdinglist(Of DAILY)

DAILY is a class create by linq

1

There are 1 best solutions below

0
On

Making a few assumptions of the code that is not shown...is the list part of a class that has a relationship in EF? If so, adding it to the list and then saving it should provide the expected result. Which is,add a new item to the list, ensuring the relationship, and saving it via EF.