When saving multiple photos, I get the error Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

1

I have a problem saving multiple photos, I get the error Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

I have a modal where I keep multiple photos:

tables: PHOTO -NOTICIA_FOTO (intermediate table) -NOTICIA When I select the image one by one (when I press the browse button), I save them, so far so good, but sometimes, when I select several images at once, and after one, I press save I get the following error:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

I show you the whole code:

<HttpPost>
Function GuardarNoticia(notic As NOTICIA) As ActionResult
    Using db As New BD_LOSCOPIHUESEntities1
        Dim fecha_actual As String = Date.Now
        Dim foto_binaria As Byte() = Nothing
        Dim titulo As String = notic.TituloNoticia 
        Dim descripcion As String = notic.DescripcionNoticia 
        Dim retorno As Integer = 1
        Dim noticia As New NOTICIA
        noticia.TituloNoticia = titulo
        noticia.DescripcionNoticia = descripcion
        noticia.FechaPublicacionNoticia = fecha_actual
        noticia.Rut = "11.111.111"
        Try
            Dim directorio_noticia As String = "/Archivos/Noticias/Fotos/" & Date.Now.ToString("dd-MM-yyyy") & "/" & titulo & "/"
            Dim directorio_noticia_portada As String = "/Archivos/Noticias/Fotos/" & Date.Now.ToString("dd-MM-yyyy") & "/" & titulo & "/Portada/"

            Dim existeDirectorioNoticia As Boolean
            existeDirectorioNoticia = System.IO.Directory.Exists(directorio_noticia)

            Dim existeDirectorioNoticiaPortada As Boolean
            existeDirectorioNoticiaPortada = System.IO.Directory.Exists(directorio_noticia_portada)

            If existeDirectorioNoticia = False Then
                Directory.CreateDirectory(Server.MapPath("/" & directorio_noticia))
            End If
            If existeDirectorioNoticiaPortada = False Then
                Directory.CreateDirectory(Server.MapPath("/" & directorio_noticia_portada))
            End If

            For i As Integer = 0 To Request.Files.Count - 1
                Dim file = Request.Files(i)

                If (file.ContentLength > 0) Then

                    Dim fileName = Path.GetFileName(file.FileName)
                    Dim formato = System.IO.Path.GetExtension(file.FileName)
                    Using reader = New System.IO.BinaryReader(file.InputStream)
                        foto_binaria = reader.ReadBytes(file.ContentLength)
                        noticia.FOTO.Add(New FOTO() With {.FechaFoto = Date.Now, _
                                                          .FormatoFoto = formato, _
                                                          .Foto = foto_binaria, _
                                                          .NombreFoto = fileName, _
                                                          .RutaFoto = "../.." & directorio_noticia & fileName})
                        If i = 0 Then
                            noticia.FotoPortadaNoticia = foto_binaria
                            noticia.NombreFotoPortadaNoticia = fileName
                            noticia.FormatoFotoPortadaNoticia = formato
                            noticia.RutaFotoPortadaNoticia = "../.." & directorio_noticia_portada & fileName
                            Dim path__portada = Path.Combine(Server.MapPath("~" & directorio_noticia_portada), fileName)
                            file.SaveAs(path__portada)
                        End If
                        Dim path__1 = Path.Combine(Server.MapPath("~" & directorio_noticia), fileName)
                        file.SaveAs(path__1)
                    End Using

                End If
            Next


            db.NOTICIA.Add(noticia)
            db.SaveChanges()

            Return Json(retorno)
        Catch ex As Exception
            retorno = 2
            Return Json(ex.Message)
        End Try


    End Using
End Function

I thought that the attached photos did not come, but if they come, it is difficult for me to find specifically the cause of my error. If anyone knows, I would appreciate it very much

    
asked by Danilo 23.01.2016 в 03:08
source

2 answers

2

You should define a try..catch that controls the DbEntityValidationException , then you can retrieve the messages from EntityValidationErrors and know what the real problem is.

There are several ways to achieve it:

The objective is to be able to have the description of the error messages in the validations to know the problem and solve it.

    
answered by 23.01.2016 / 14:21
source
0

Excellent Leandro. I was able to capture the error by defining the try..cach that controls Entity.Validation.DbEntityValidationException:

try
...
Catch ex As Entity.Validation.DbEntityValidationException
            Dim errorMessages =    ex.EntityValidationErrors.SelectMany(Function(x) x.ValidationErrors).Select(Function(x) x.ErrorMessage)
            Dim fullErrorMessage = String.Join("; ", errorMessages)
            Dim exceptionMessage = String.Concat(ex.Message, " La validacion del error es: ", fullErrorMessage)
            Throw New Entity.Validation.DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors)                
        End Try

In my case, the cause of the error was because the size of a table's field was very small and it had larger names.

    
answered by 25.01.2016 в 04:15