Bug #48417 Invalid cast from 'System.String' to 'System.Guid'
Submitted: 29 Oct 2009 18:36 Modified: 12 Mar 2010 21:04
Reporter: Waaahsabi -- Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:6.1.2 OS:Windows (Windows XP and 7)
Assigned to: Reggie Burnett CPU Architecture:Any
Tags: cast, CHAR(36), entity, entity framework, Framework, Guid

[29 Oct 2009 18:36] Waaahsabi --
Description:
Prerequisites:
- Table using CHAR(36) BINARY column as PK to hold values of type Guid
- Using MS Entity Framework

When using CHAR(36)BINARY as column type in order to use Guids as identifiers AND when not utilizing "old guids=true" in the connection string, there will be an exception in:

Project: MySQL.Data.Entity
File: EFMySqlDataReader.cs
Method: private object ChangeType(object sourceValue, Type targetType).

because the method will use System.Convert("theguidstring", typeof(Guid)), which fails.

How to repeat:
see desc.

Suggested fix:
replace the method body with:

private object ChangeType(object sourceValue, Type targetType)
{
    ///FIX: Invalid cast for new-style GUIDs
    if(sourceValue is string && targetType == typeof(Guid))
    {
        return new Guid((string) sourceValue);
    }

    if (sourceValue is byte[] && targetType == typeof(Guid))
    {
        return new Guid((byte[])sourceValue);
    }

    if (sourceValue is DateTime && targetType == typeof(DateTimeOffset))
    {
        return new DateTimeOffset((DateTime)sourceValue);
    }

    return Convert.ChangeType(sourceValue, targetType, CultureInfo.InvariantCulture);
}
[30 Oct 2009 12:28] Tonci Grgin
Hi Waaahsabi and thanks for your report.

Verified by looking into latest sources. IMO, should be considered with Bug#47985.
[2 Nov 2009 20:59] Reggie Burnett
Columns that are to hold guids should be not char(36) binary but rather just char(36).  Please try the columns as non-binary.
[2 Nov 2009 22:34] Waaahsabi --
Ok, i just tried it with CHAR(36) non-binary, but the behavior is the same. String is being converted into Guid via System.Convert(), which fails. Must be "new Guid(theString)" instead.
[3 Nov 2009 16:09] Reggie Burnett
The fix for bug #47985 also fixed this.  My assumption is that the poster of this bug is using some form of multi-byte charset.  Bug #47985 illustrated that multi-byte cs caused char(36) columns to not return as guid but instead return as string which would trigger the effect seen here.
[3 Nov 2009 16:11] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/89157

785 Reggie Burnett	2009-11-03
      A few changes to include a test to verify bug #48417 has been fixed.
[3 Nov 2009 16:13] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/89160

793 Reggie Burnett	2009-11-03 [merge]
      A few changes to include a test to verify bug #48417 has been fixed.
[3 Nov 2009 16:19] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/89163

793 Reggie Burnett	2009-11-03 [merge]
      A few changes to include a test to verify bug #48417 has been fixed.
[12 Mar 2010 15:47] Rodrigo Hentz
how I can fix this in my project?
[12 Mar 2010 19:49] Rodrigo Hentz
Waaahsabi How you fix this error? I have the same problem im my application. thanks.
[12 Mar 2010 21:04] Waaahsabi --
I fixed the cast itself, ie. instead of an indirect cast from string to GUID i did "new GUID(stringValue);"

But then i noticed that the whole Connector.NET for MySQL + WorkBench + EntityFramework is a pretty unstable and unreliable combination, so i switched to SQL Server 2k8.