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