Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I post here my code
VB
Public Shared Function delete_config(ByVal configurazione As CanoniConfig) As Boolean
       Dim result As Boolean = False
       Dim configurazione_to_delete As ConfigurazioniCanoniFissi = Nothing

       Try
           Using dbContext As New P2000ProdwareEntities
               configurazione_to_delete = getFirstConfig(configurazione)
               If Not configurazione_to_delete Is Nothing Then
                   dbContext.DeleteObject(configurazione_to_delete)
                   dbContext.SaveChanges()
               End If
               result = True
           End Using
       Catch ex As Exception
           result = False
       End Try
       Return result
   End Function

configurazione_to_delete is the instance to the row that need to be deleted and it's not nothing.

If I run this code I obtain this
"Can not delete the object because it was not found in ObjectStateManager."

What is wrong?
Thank you!
Posted
Updated 21-Jul-15 21:54pm
v2

1 solution

Done.
The problem was that the operation of updating or deleting rows must be
l between using statement so if you get the row with getfirst config your modifying that have no effect.
so the code is this



Public Shared Function delete_config(ByVal configurazione As CanoniConfig) As Boolean
    Dim result As Boolean = False
    Dim configurazione_to_delete As ConfigurazioniCanoniFissi = Nothing
    Try
        Using dbContext As New P2000ProdwareEntities
            configurazione_to_delete = (From e In dbContext.ConfigurazioniCanoniFissis
                                  Where e.ChFase = configurazione.chfase And e.ChTecnica = configurazione.chtecnica And e.Commessa = configurazione.commessa And e.ChCliente = configurazione.chcliente
                                  Select e).First
            If Not configurazione_to_delete Is Nothing Then
               dbContext.DeleteObject(configurazione_to_delete)
                dbContext.SaveChanges()
            End If
            result = True
        End Using
    Catch ex As Exception
        result = False
    End Try
    Return result
End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900