Vb.net- An Exception of type 'System.Data.Entity.Infrastructure.Dbupdateexception'

Asked

Viewed 37 times

0

I created a delete but when executing the error :

An Exception of type 'System.Data.Entity.Infrastructure.Dbupdateexception', I’ve looked for help, but all that appears is for #C, I need a solution for VB.

I think the problem is that the default table is associated with a process, the process table. And the program does not let delete to not leave the process "hanging"...

 ' GET: DEF_DEFECT/Delete/5
    Function Delete(ByVal id As String) As ActionResult
        If IsNothing(id) Then
            Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
        End If
        Dim dEF_DEFECT As DEF_DEFECT = db.DEF_DEFECT.Find(id)
        If IsNothing(dEF_DEFECT) Then
            Return HttpNotFound()
        End If
        Return View(dEF_DEFECT)
    End Function

    ' POST: DEF_DEFECT/Delete/5
    <HttpPost()>
    <ActionName("Delete")>
    <ValidateAntiForgeryToken()>
    Function DeleteConfirmed(ByVal id As String) As ActionResult
        Dim dEF_DEFECT As DEF_DEFECT = db.DEF_DEFECT.Find(id)
        db.DEF_DEFECT.Remove(dEF_DEFECT)
        db.SaveChanges()
        Return RedirectToAction("Index")
    End Function

inserir a descrição da imagem aqui

  • What is the exception message?

  • I edited to add photo error

1 answer

0


Through the forum ASP.NETI came to the conclusion that I should change the code to:

' POST: DEF_DEFECT/Delete/5
    <HttpPost()>
    <ActionName("Delete")>
    <ValidateAntiForgeryToken()>
    Function DeleteConfirmed(ByVal id As String) As ActionResult
        Dim dEF_DEFECT As DEF_DEFECT = db.DEF_DEFECT.Find(id)
        For Each pRO_PROCESS In dEF_DEFECT.PRO_PROCESS
            db.PRO_PROCESS.Remove(pRO_PROCESS)
            db.DEF_DEFECT.Remove(dEF_DEFECT)
            db.SaveChanges()

        Next
        Return RedirectToAction("Index")
    End Function`

But the error persisted, after much research, I realized that the problem was not only the relation of the defects with the processes, but obviously that it also had the relation of the defects with the causes. So I changed the code to:

' POST: DEF_DEFECT/Delete/5
    <HttpPost()>
    <ActionName("Delete")>
    <ValidateAntiForgeryToken()>
    Function DeleteConfirmed(ByVal id As String) As ActionResult
        Dim dEF_DEFECT As DEF_DEFECT = db.DEF_DEFECT.Find(id)

        If dEF_DEFECT.PRO_PROCESS.Count = 0 And dEF_DEFECT.CAU_CAUSE.Count = 0 Then
            db.DEF_DEFECT.Remove(dEF_DEFECT)
            db.SaveChanges()
        Else
            Dim deleteListCauses As List(Of FMEA_MVC.CAU_CAUSE) = New List(Of FMEA_MVC.CAU_CAUSE)
            deleteListCauses = dEF_DEFECT.CAU_CAUSE.ToList()
            For item As Integer = 0 To deleteListCauses.Count - 1 Step 1
                dEF_DEFECT.CAU_CAUSE.Remove(deleteListCauses(item))
            Next
            Dim deleteListItems As List(Of FMEA_MVC.PRO_PROCESS) = New List(Of FMEA_MVC.PRO_PROCESS)
            deleteListItems = dEF_DEFECT.PRO_PROCESS.ToList()

            For item As Integer = 0 To deleteListItems.Count - 1 Step 1
                dEF_DEFECT.PRO_PROCESS.Remove(deleteListItems(item))
            Next
            MsgBox("You have one or more processes and causes associated" & Chr(13) & "" & Chr(13) & "Are you sure you want to delete them?", vbYesNo, "Delete Defect")
            db.DEF_DEFECT.Remove(dEF_DEFECT)
            db.SaveChanges()
        End If

        Return RedirectToAction("Index")
    End Function

And problem solved :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.