0

I had a code working on a localdb SQL Server DataBase, using Entity Framework 6 and DataBase first aproach. One of the tasks of this code is to insert values into a master-detail tables. Now I have to migrate the solution into MySQL, and I getting a DbUpdateException wich I know is caused by the detail table.

I've got this Entity classes:

Partial Public Class GSPC_Resultado
    Public Property ResultadoID As Long
    Public Property Fecha As Date
    Public Property ConfiguracionID As Long

    Public Overridable Property GSPC_Configuracion As GSPC_Configuracion
    Public Overridable Property GSPC_ResultadoDetalle As ICollection(Of     GSPC_ResultadoDetalle) = New HashSet(Of GSPC_ResultadoDetalle)

End Class

Partial Public Class GSPC_ResultadoDetalle
    Public Property ResultadoDetalleID As Long
    Public Property ResultadoID As Long
    Public Property RutaID As Long
    Public Property NombreArchivo As String
    Public Property AccionCompletada As Nullable(Of SByte)

    Public Overridable Property GSPC_Resultado As GSPC_Resultado
    Public Overridable Property GSPC_ResultadoRuta As GSPC_ResultadoRuta

End Class

Let's say I have a code to insert master-detail like that (simplified):

Dim resultado As New GSPC_Resultado
resultado.ConfiguracionID = 1 
resultado.Fecha = DateTime.Now

Dim resultadoDetalle As New GSPC_ResultadoDetalle
resultadoDetalle.RutaID = 1 
resultadoDetalle.NombreArchivo = "myname"
resultadoDetalle.AccionCompletada = True

resultado.GSPC_ResultadoDetalle.Add(resultadoDetalle)

context.GSPC_ResultadoSet.Add(resultado)
context.SaveChanges()

Trying to catch the exception to find the issue:

Try
    context.SaveChanges()
    Catch ex As DbUpdateException

    For Each entry As DbEntityEntry In ex.Entries
        Console.WriteLine("Type {0} ", entry.Entity.GetType().Name)
    Next
End Try

With this I know that the entity GSPC_ResultadoDetalle is causing the exceptyion, but I do not see wich field is causing this exception. Some exception properties are:

  • Message: "An error occurred while updating the entries. See the InnerException for details."
  • InnerException: The specified value is not an instance of a valid constant type.

As I said, this worked on a SQL Server (localdb), but not on MySQL. Unfortunately I'm not able to identify with field of GSPC_ResultadodDetalle is causing the exception, but I suspect that GSPC_ResultadoDetalle is unable to get the ResultadoID value from GSPC_Resultado.

Any suggestions?

Thank's in advice,

Ferran.

Ferran
  • 189
  • 2
  • 7
  • add stack trace exception – M. Wiśnicki Dec 28 '16 at 10:43
  • Seems a issue related to AccionCompletada field, wich is a tinyint(1) field on database, and is treated like a Sbyte (bool) on entity framework. However, trying the solution suggested on http://stackoverflow.com/questions/23731963/ado-entity-treat-tiny-as-boolean-false do not work for me. – Ferran Dec 28 '16 at 12:45

1 Answers1

0

Well, finally I've got to change my code, to force entity framework to save changes after add information to master table, and then add details and finally save changes again.

Dim resultado As New GSPC_Resultado
resultado.ConfiguracionID = 1 
resultado.Fecha = DateTime.Now
context.GSPC_ResultadoSet.Add(resultado)
context.SaveChanges()

Dim resultadoDetalle As New GSPC_ResultadoDetalle
resultadoDetalle.RutaID = 1 
resultadoDetalle.NombreArchivo = "myname"
resultadoDetalle.AccionCompletada = True
resultadoDetalle.GSPC_Resultado = resultado

context.GSPC_ResultadoDetalleSet.Add(resultadoDetalle)
context.SaveChanges()

I've got to mention that, when I migrated the DB from SQL Server to MySQL I have changed from EF6 to EF5. I don't know if this issue is related to Entity Framework version, or Mysql Connector, or......

Cheers,

Ferran.

Ferran
  • 189
  • 2
  • 7