Bug #63699 Invalid cast exception when using .Include for optionally linked tables
Submitted: 9 Dec 2011 17:16 Modified: 9 Jan 2012 21:44
Reporter: Talbot McInnis Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.4.4 OS:Any
Assigned to: Bogdan Degtyariov CPU Architecture:Any
Tags: Exception Include

[9 Dec 2011 17:16] Talbot McInnis
Description:
I am using .NET connection from a C#.net WPF application and the entity framework.

This query works:
List<Song> DBSongs = bmnce.Songs
.Include("SongArtistsInSongs.SongArtist")
.Include("SongArtistsFeaturedInSongs.SongArtist")
.Where(s => results.Contains(s.ID)).ToList();

Note that not all rows in Songs have entries in SongArtistsFeaturedInSongs.

I am now adding an additional include to another table linked to songs:
List<Song> DBSongs = bmnce.Songs
.Include("SongsInCategories.SongCategory")
.Include("SongArtistsInSongs.SongArtist")
.Include("SongArtistsFeaturedInSongs.SongArtist")
.Where(s => results.Contains(s.ID)).ToList();

This query works if and only if the data being fetched does NOT have an entry in SongArtistsFeaturedInSongs.  If it does, then I get an InvalidCastException saying the object must implement IConvertable.  I've dug into the underlying SQL and for the rows from the SongArtistsFeaturedInSongs table, MySQLworkbench reports them as BLOB type even though when I use the data visualizer they are all integers (IDs, mostly).

How to repeat:
Create three tables, Songs, SongCategories and SongArtists.  Create linking tables for each, (Song -> SongArtist has two linking tables, SongArtistsInSongs and SongArtistsFeaturedInSongs).  Create a song and create an entry in each linking table, and then run the entity query shown above.

Suggested fix:
Ensure that data which starts as integers is never converted to a BLOB in the queries that the connectoer generates.
[9 Dec 2011 17:54] Talbot McInnis
The SQL query generated by the entity framework which produced the BLOB

Attachment: Offending SQL.sql (application/octet-stream, text), 7.40 KiB.

[9 Dec 2011 18:03] Talbot McInnis
SQL to create tables (without data)

Attachment: Create tables.sql (application/octet-stream, text), 3.75 KiB.

[14 Dec 2011 10:33] Bogdan Degtyariov
Hi Talbot,

Thanks for the detailed problem description.
Can you provide the data for the tables involved in the query to ensure that logical links are set correctly?
[19 Dec 2011 9:43] Bogdan Degtyariov
Talbot,

Thanks for sending the table data.
I created the test project and found a few issues there.
Your LINQ query is referencing objects not mentioned anywhere, so I did some guesswork to avoid the compiler errors:

1. What is "s" in Where(s => results.Contains(s.ID))?

I guessed it is a Song:

Song s = bmnce.Songs.First();

2. What is "results"? It can be literally anything, so I did not come with
   any guess.

Also, I run the offending query in the workbench and none of columns were BLOB type. This could be the server problem on your side since the actual issue is related to BLOB instead of INT in the results. I used MySQL Server 5.5.19.
[19 Dec 2011 19:20] Talbot McInnis
The server version may be the major difference.  I am upgrading one of my slaves to 5.5.19 and then I'll run the test query.  If I do not get the BLOB results then I think we've found our issue.  I will let you know.
[19 Dec 2011 20:15] Talbot McInnis
I upgraded our slave to 5.5.19 and the data no longer returns as BLOB.  Would you like to consider this resolved, or should the connector handle this case more gracefully?
[20 Dec 2011 5:04] Bogdan Degtyariov
Which version of MySQL Server returned BLOB columns?
I tested with 5.1.60 and it was fine as well.
[20 Dec 2011 14:20] Talbot McInnis
5.0.77.  This was the version recommended by Rackspace as the most stable version (2 years ago, and they do not apply updates unless asked).
[23 Dec 2011 7:48] Bogdan Degtyariov
Talbot,

thanks for your help and interest in MySQL.

MySQL Server Version 5.0 is not considered high-demand, so the bug is closed.
[9 Jan 2012 21:44] Talbot McInnis
In case anyone else encouters this, I do want to confirm that after upgrading our servers to 5.5.19 I no longer experience this bug.