簡體   English   中英

MySQL數據提供程序-無法將類型為“ System.Byte []”的對象轉換為類型為“ System.IConvertible”

[英]MySQL Data Provider - Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'

我在實體框架上下文中遇到了一些困難,事實證明調試起來非常麻煩。 昨天我在我的應用程序中添加了一個功能,該功能為我的一個實體提供了一個子實體的附加集合(稱為模型),並且在我查詢父對象時將我的一個表達式更新為Include()集合之后,我的查詢失敗當另一個之前運行良好的集合中的一個是Include()時,則返回InvalidCastException。

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);

我弄弄了這個表達式,刪除了不同的集合/更改了Include()的子項,發現正是這些項的確切組合導致了上述異常。 如果刪除任何這些包括()沒有什么異常,但與三個孩子還有每次我試圖去拉已經在一個或多個實體的單一產品時間是一個Colors的集合。 如果刪除FirstOrDefault(p => p.ID == id)或Colors集合為空,則不會引發異常。

看起來,添加Models集合是我查詢的起點,但是我真的不確定為什么。

異常的確切來源是Mysql.Data並且stacktrace讀取:

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)

實體定義(僅相關字段)

產品

  • int ID
  • EntityCollection<ProductColorOption>顏色
  • EntityCollection<ProductImage>圖片
  • EntityCollection<ProductModel>模型

ProductColor

  • int ID

ProductColorOption

  • int ID
  • int ProductID
  • int ProductColorID

產品型號

  • int ID
  • int ProductID

ProductImage

  • int ID
  • int ProductID
  • sbyte? 訂購

關系

  • Product.ID FK ProductImage.ProductID (一對多)
  • Product.ID FK ProductColorOption.ProductID (一對多)
  • Product.ID FK ProductModel.ProductID (一對多)
  • ProductColor.ID FK ProductColorOption.ProductColorID (一對多)

謝謝大家!


更新

gaustin的建議在下面解決了問題(沒有更多例外),但並未真正針對實際問題。 因此,我將問題保留為“未回答”。

更新2

現在,我意識到該錯誤與實體框架無關,而與MySQL數據提供程序有關。 由於某種原因,它將ProductColor.ID視為byte [],然后嘗試將其盲目轉換為int。 這根本不是實體框架問題。 詛咒您 Sun Microsystems ORACLE。

您是否嘗試過延遲加載包含項?

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

// ... and so on

那可能是有益的。

這似乎也是一個已知問題 我不確定目前是否有已知的解決方案。 如果沒有,我找不到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM