Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 351185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:37:12+00:00 2026-05-12T11:37:12+00:00

I’m having some difficulty with my Entity Framework context that is proving very troublesome

  • 0

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.

Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'

var b = DbEntities.Products.Include("Images").Include("Models").Include("Colors").FirstOrDefault(p => p.ID == id);

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 any of those Includes() there is no exception, but with those three children there is one every time I try to pull a single Product that has one or more entity in the Colors collection. The exception is not thrown if I remove the FirstOrDefault(p => p.ID == id) or if the Colors collection is empty.

It would seem that the addition of the Models collection was the breaking point for my query, but I am really not sure why.

The exact source of the exception is Mysql.Data and the stacktrace reads:

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.<GetElementFunction>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.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

Entity Definitions (relevant fields only)

Product

  • int ID
  • EntityCollection<ProductColorOption> Colors
  • EntityCollection<ProductImage> Images
  • EntityCollection<ProductModel> Models

ProductColor

  • int ID

ProductColorOption

  • int ID
  • int ProductID
  • int ProductColorID

ProductModel

  • int ID
  • int ProductID

ProductImage

  • int ID
  • int ProductID
  • sbyte? Order

Relationships

  • Product.ID FK ProductImage.ProductID (One to Many)
  • Product.ID FK ProductColorOption.ProductID (One to Many)
  • Product.ID FK ProductModel.ProductID (One to Many)
  • ProductColor.ID FK ProductColorOption.ProductColorID (One to Many)

Thanks all!


Update

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’.

Update 2

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 Sun Microsystems ORACLE.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:37:12+00:00Added an answer on May 12, 2026 at 11:37 am

    Have you tried lazy loading the includes?

    var product = DbEntities.Products.FirstOrDefault(p => p.Id == id);
    product.Images.Load();
    
    // ... and so on
    

    That might be informative.

    This also looks to be a known issue. I’m not sure if there’s a known resolution at the moment. If there is I can’t find it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 176k
  • Answers 176k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As Michael Burr said, while the overhead strongly depends on… May 12, 2026 at 3:19 pm
  • Editorial Team
    Editorial Team added an answer The compiler can do as it pleases when it comes… May 12, 2026 at 3:19 pm
  • Editorial Team
    Editorial Team added an answer Yes, Web URL does not have to correspond to the… May 12, 2026 at 3:19 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.