title: MySQL Data Provider - Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'
body: <p>I'm having some difficulty with my Entity Framework context that is proving very troublesome to debug. I added a feature to my application yesterday that gave one of my entities an additional collection of child entities (called Models) and after updating one of my expressions to Include() that collection when I query for the parent object, my query is failing with an InvalidCastException when another one of the collections which worked fine before is Include()'d.</p>
  
  <pre><code>Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'
  </code></pre>
  
  <hr>
  
  <pre><code>var b = DbEntities.Products.Include("Images").Include("Models").Include("Colors").FirstOrDefault(p =&gt; p.ID == id);
  </code></pre>
  
  <p>I fiddled with the expression and removed different collections/changed the Include()'d children and discovered it is this exact combination of items that causes the aforementioned exception.  If I remove <em>any</em> of those Includes() there is no exception, but with those three children there is one every time I try to pull a <strong>single</strong> Product that has <strong>one or more</strong> entity in the <code>Colors</code> collection. The exception is not thrown if I remove the FirstOrDefault(p => p.ID == id) or if the <code>Colors</code> collection is empty.</p>
  
  <p>It would seem that the addition of the Models collection was the breaking point for my query, but I am really not sure why.</p>
  
  <p>The exact source of the exception is <code>Mysql.Data</code> and the stacktrace reads:</p>
  
  <pre><code>at MySql.Data.MySqlClient.MySqlDataReader.GetInt32(Int32 i)
  at lambda_method(ExecutionScope , Shaper )
  at System.Data.Common.Internal.Materialization.Coordinator.HasNextElement(Shaper shaper)
  at System.Data.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MoveNext()
  at System.Data.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.TryReadToNextElement()
  at System.Data.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.ReadElement()
  at System.Data.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.MoveNext()
  at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
  at System.Data.Objects.ELinq.ObjectQueryProvider.&lt;GetElementFunction&gt;b__1[TResult](IEnumerable`1 sequence)
  at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
  at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
  at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
  at Data.ProductsRepository.GetProduct(Int32 id) in ProductsRepository.cs:line 65
  at Web.Controllers.Areas.Admin.ProductsController.EditProduct(Int32 id) in ProductsController.cs:line 111
  at lambda_method(ExecutionScope , ControllerBase , Object[] )
  at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
  at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClassa.&lt;InvokeActionMethodWithFilters&gt;b__7()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  </code></pre>
  
  <h2>Entity Definitions (relevant fields only)</h2>
  
  <p><strong>Product</strong></p>
  
  <ul>
  <li><code>int</code> ID</li>
  <li><code>EntityCollection&lt;ProductColorOption&gt;</code> Colors</li>
  <li><code>EntityCollection&lt;ProductImage&gt;</code> Images</li>
  <li><code>EntityCollection&lt;ProductModel&gt;</code> Models</li>
  </ul>
  
  <p><strong>ProductColor</strong> </p>
  
  <ul>
  <li><code>int</code> ID</li>
  </ul>
  
  <p><strong>ProductColorOption</strong></p>
  
  <ul>
  <li><code>int</code> ID</li>
  <li><code>int</code> ProductID</li>
  <li><code>int</code> ProductColorID</li>
  </ul>
  
  <p><strong>ProductModel</strong></p>
  
  <ul>
  <li><code>int</code> ID</li>
  <li><code>int</code> ProductID</li>
  </ul>
  
  <p><strong>ProductImage</strong></p>
  
  <ul>
  <li><code>int</code> ID</li>
  <li><code>int</code> ProductID</li>
  <li><code>sbyte?</code> Order</li>
  </ul>
  
  <h2>Relationships</h2>
  
  <ul>
  <li>Product.ID <code>FK</code> ProductImage.ProductID <em>(One to Many)</em></li>
  <li>Product.ID <code>FK</code> ProductColorOption.ProductID <em>(One to Many)</em></li>
  <li>Product.ID <code>FK</code> ProductModel.ProductID <em>(One to Many)</em></li>
  <li>ProductColor.ID <code>FK</code> ProductColorOption.ProductColorID <em>(One to Many)</em></li>
  </ul>
  
  <p>Thanks all!</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>gaustin's suggestion below fixes problem (no more exception), but doesn't really target the actual issue. For that reason I'm going to leave the question 'unanswered'.</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>I realize now that the bug pertains not to Entity Framework, but rather to the MySQL Data Provider. For some reason it is treating ProductColor.ID as a byte[] and then trying to cast it blindly to an int. This is not an Entity Framework problem at all. Curse you <strike>Sun Microsystems</strike> ORACLE.</p>