vb.net- An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException'

0

I created a delete but when I run the error it gives me:

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException', I have looked for help, but everything that appears is for #C, I need a solution for VB.

I think the problem is that the defect table is associated with a process, from the process table. And the program does not let you 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

    
asked by anonymous 03.11.2017 / 17:53

1 answer

0

Through the ASP.NET forum I 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 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

    
07.11.2017 / 11:26