我有一些困难,我实体框架背景下,证明是非常麻烦的调试。我增加了一个功能,我昨天给了我一个实体的额外收集的子实体(称为模型),并在更新之后我的一个表现,包括()就收集当我查询父目的,我的查询是没有InvalidCastException当一个又一个的收集工作的现之前是包括()'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);

我摆弄的表达和删除,不同的集合/改变了包括()'d的儿童和发现的正是这种精确组合的项目,导致上述例外。如果我消除 任何 这些包括()没有例外,但与这三个儿童有一个每次我尝试拉一个 产品 一个或更多 实体在 Colors 收集。唯一的例外是不扔如果我删除FirstOrDefault(p=>。ID==id)或者如果 Colors 集合是空的。

这似乎是另外的模型集合的是突破点我的查询,但是我真的不知道为什么。

的确切来源的唯一的例外是 Mysql.Data 和堆栈跟踪读取:

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?

关系

  • 产品。ID FK ProductImage.ProductID (一至多)
  • 产品。ID FK ProductColorOption.ProductID (一至多)
  • 产品。ID FK 产品型号.ProductID (一至多)
  • ProductColor.ID FK ProductColorOption.ProductColorID (一至多)

谢谢!


更新

gaustin的以下建议修正问题(没有更多的例外),但并没有真正目标的实际问题。由于这个原因我要离开的问题没有答案'.

更新2

我现在意识到这一错误涉及不到的实体的框架,而是要MySQL数据提供商。由于某些原因,它是治疗ProductColor.ID作为一个字节[],然后试图把它盲目到一个int.这不是一个实体框架问题。诅咒你 Sun微系统公司 ORACLE。

有帮助吗?

解决方案

你有没有尝试过偷懒装载的包括?

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

// ... and so on

这可能是内容丰富。

这个看起来也是 已知的问题.我不确定如果有一个已知的决议的时刻。如果有我找不到它。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top