Bug #59824 InvalidCastException thrown when loading related entities
Submitted: 30 Jan 2011 19:44 Modified: 23 May 2011 16:50
Reporter: Robert Hood Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.3.6 OS:Microsoft Windows
Assigned to: Julio Casal CPU Architecture:Any

[30 Jan 2011 19:44] Robert Hood
Description:
An InvalidCastException (Object must implement IConvertible) is thrown then loading related entities against MySQL 5.0.77.  Error does not occur when connecting to MySQL 5.0.45

Stack Trace:

   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at MySql.Data.Entity.EFMySqlDataReader.ChangeType(Object sourceValue, Type targetType)
   at MySql.Data.Entity.EFMySqlDataReader.GetValue(Int32 ordinal)
   at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
   at System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
   at lambda_method(ExecutionScope , Shaper )

How to repeat:
Attached is a zip file containing a VS 2010 project and a DB Dump for a database to reproduce the issue.

Suggested fix:
Debug, find error.
[30 Jan 2011 19:46] Robert Hood
Zip file containing demo project to show the bug

Attachment: MySQLBugRepro.zip (application/x-zip-compressed, text), 22.67 KiB.

[7 Feb 2011 9:31] Thomas Eixner
I have the same problem when loading entities in a model with inheritance in the Graph. 

It seems others are also facing this issue:
http://forums.mysql.com/read.php?38,359034,359034

A workaround is to load every sub-type separately by using .OfType<type>(). This works but ends up in a really bad performance!
[14 Feb 2011 21:03] Santo Leto
Thank you for a problem report.

I can see the exception on versions 5.0.77 and 5.1.54. I don't see it on versions 5.0.45 and 5.5.8.

* 5.0.45:

D:\tmp\bug59824\MySQLBugRepro\MySQLBugRepro\MySQLBugRepro\bin\Debug>MySQLBugRepro.exe
BIODIESEL LOADOUT has 1 pagers(s)
        Type: IP_SIGN_PAGING_DEVICE
Oil Loading 2 has 1 pagers(s)
        Type: IP_SIGN_PAGING_DEVICE
Oil Loadout has 1 pagers(s)
        Type: IP_SIGN_PAGING_DEVICE
Loading Station1 has 0 pagers(s)
Loading Station1 has 1 pagers(s)
        Type: IP_SIGN_PAGING_DEVICE
Corn Dump 1 has 1 pagers(s)
        Type: IP_SIGN_PAGING_DEVICE
Corn Dump 2 has 0 pagers(s)
Corn Dump 3 has 0 pagers(s)
^C
D:\tmp\bug59824\MySQLBugRepro\MySQLBugRepro\MySQLBugRepro\bin\Debug>

* 5.0.77:

D:\tmp\bug59824\MySQLBugRepro\MySQLBugRepro\MySQLBugRepro\bin\Debug>MySQLBugRepro.exe

Eccezione non gestita: System.InvalidCastException: L'oggetto deve implementare
IConvertible.
   in System.Convert.ChangeType(Object value, Type conversionType, IFormatProvid
er provider)
   in MySql.Data.Entity.EFMySqlDataReader.ChangeType(Object sourceValue, Type ta
rgetType)
   in MySql.Data.Entity.EFMySqlDataReader.GetValue(Int32 ordinal)
   in System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReade
r`1.GetValue(DbDataReader reader, Int32 ordinal)
   in System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErr
orHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
   in lambda_method(ExecutionScope , Shaper )
   in System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[
TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySe
t)
   in lambda_method(ExecutionScope , Shaper )
   in System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(
Shaper shaper)
   in System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.Move
Next()
   in System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   in System.Data.Objects.DataClasses.RelatedEnd.Merge[TEntity](IEnumerable`1 co
llection, MergeOption mergeOption, Boolean setIsLoaded)
   in System.Data.Objects.DataClasses.EntityCollection`1.Load(IEnumerable`1 coll
ection, MergeOption mergeOption)
   in System.Data.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeO
ption)
   in System.Data.Objects.DataClasses.RelatedEnd.Load()
   in MySQLBugRepro.Program.<Main>b__0(LOADING_STATION p) in D:\tmp\bug59824\MyS
QLBugRepro\MySQLBugRepro\MySQLBugRepro\Program.cs:riga 18
   in System.Collections.Generic.List`1.ForEach(Action`1 action)
   in MySQLBugRepro.Program.Main(String[] args) in D:\tmp\bug59824\MySQLBugRepro
\MySQLBugRepro\MySQLBugRepro\Program.cs:riga 16

D:\tmp\bug59824\MySQLBugRepro\MySQLBugRepro\MySQLBugRepro\bin\Debug>

* 5.1.54:

Eccezione non gestita: System.InvalidCastException: L'oggetto deve implementare
IConvertible.
   in System.Convert.ChangeType(Object value, Type conversionType, IFormatProvid
er provider)
   in MySql.Data.Entity.EFMySqlDataReader.ChangeType(Object sourceValue, Type ta
rgetType)
   in MySql.Data.Entity.EFMySqlDataReader.GetValue(Int32 ordinal)
   in System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReade
r`1.GetValue(DbDataReader reader, Int32 ordinal)
   in System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErr
orHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
   in lambda_method(ExecutionScope , Shaper )
   in System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[
TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySe
t)
   in lambda_method(ExecutionScope , Shaper )
   in System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(
Shaper shaper)
   in System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.Move
Next()
   in System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   in System.Data.Objects.DataClasses.RelatedEnd.Merge[TEntity](IEnumerable`1 co
llection, MergeOption mergeOption, Boolean setIsLoaded)
   in System.Data.Objects.DataClasses.EntityCollection`1.Load(IEnumerable`1 coll
ection, MergeOption mergeOption)
   in System.Data.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeO
ption)
   in System.Data.Objects.DataClasses.RelatedEnd.Load()
   in MySQLBugRepro.Program.<Main>b__0(LOADING_STATION p) in D:\tmp\bug59824\MyS
QLBugRepro\MySQLBugRepro\MySQLBugRepro\Program.cs:riga 18
   in System.Collections.Generic.List`1.ForEach(Action`1 action)
   in MySQLBugRepro.Program.Main(String[] args) in D:\tmp\bug59824\MySQLBugRepro
\MySQLBugRepro\MySQLBugRepro\Program.cs:riga 16

D:\tmp\bug59824\MySQLBugRepro\MySQLBugRepro\MySQLBugRepro\bin\Debug>

* 5.5.8:

D:\tmp\bug59824\MySQLBugRepro\MySQLBugRepro\MySQLBugRepro\bin\Debug>MySQLBugRepro.exe
BIODIESEL LOADOUT has 1 pagers(s)
        Type: IP_SIGN_PAGING_DEVICE
Oil Loading 2 has 1 pagers(s)
        Type: IP_SIGN_PAGING_DEVICE
Oil Loadout has 1 pagers(s)
        Type: IP_SIGN_PAGING_DEVICE
Loading Station1 has 0 pagers(s)
Loading Station1 has 1 pagers(s)
        Type: IP_SIGN_PAGING_DEVICE
Corn Dump 1 has 1 pagers(s)
        Type: IP_SIGN_PAGING_DEVICE
Corn Dump 2 has 0 pagers(s)
Corn Dump 3 has 0 pagers(s)
^C
D:\tmp\bug59824\MySQLBugRepro\MySQLBugRepro\MySQLBugRepro\bin\Debug>
[23 May 2011 16:50] Julio Casal
After analyzing the query produced by the Entity Framework in this scenario I found out that some of the resulting columns, which are all part of several UNION statements, will have different datatypes when changing from one server version to another. So, for instance, a column reported as LONGLONG in version 5.0.45 will be reported as STRING in 5.0.77. Also, for server version 5.0.45 a column that would come with the BINARY NUM flags, would come with BINARY flags in version 5.0.77 and with no flags at all in version 5.5.12.

This incosistency makes Connector/Net interpret non binary columns as binary in server version 5.0.77+ and 5.1.x, thus causing the InvalidCastException. So, as a workaround for this issue, please set the following additional key and value in your connection string:

respect binary flags=false

More info on this can be found in the following page:
http://dev.mysql.com/doc/refman/5.0/en/connector-net-programming-binary-issues.html

The issue does not happen anymore in server version 5.5.x.