MySQLデータプロバイダー-タイプ 'System.Byte []'のオブジェクトをタイプ 'System.IConvertible'にキャストできません

StackOverflow https://stackoverflow.com/questions/1401310

質問

Entity Frameworkコンテキストに問題があり、デバッグが非常に面倒です。昨日、アプリケーションに機能を追加して、エンティティの1つに子エンティティの追加コレクション(モデルと呼ばれる)を追加し、親オブジェクトのクエリ時に式の1つをInclude()に更新した後、クエリが失敗します以前に正常に動作したコレクションの別の1つがInclude() 'dである場合、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()された子を変更したところ、前述の例外を引き起こすのはこのアイテムの正確な組み合わせであることがわかりました。それらのIncludes()の any を削除しても例外はありませんが、これら3人の子供には、のある単一の製品をプルしようとするたびに1つのColorsコレクション内の1つ以上のエンティティ。 FirstOrDefault(p = <!> gt; p.ID == id)を削除した場合、またはMysql.Dataコレクションが空の場合、例外はスローされません。

Modelsコレクションの追加がクエリのブレークポイントになったように見えますが、実際には理由がわかりません。

例外の正確なソースはintで、スタックトレースは次のようになります。

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)

エンティティ定義(関連フィールドのみ)

製品

  • EntityCollection<ProductColorOption> ID
  • EntityCollection<ProductImage>
  • EntityCollection<ProductModel>画像
  • sbyte?モデル

ProductColor

  • FK ID

ProductColorOption

  • <=> ID
  • <=> ProductID
  • <=> ProductColorID

ProductModel

  • <=> ID
  • <=> ProductID

ProductImage

  • <=> ID
  • <=> ProductID
  • <=>注文

関係

  • Product.ID <=> ProductImage.ProductID (1対多)
  • Product.ID <=> ProductColorOption.ProductID (1対多)
  • Product.ID <=> ProductModel.ProductID (1対多)
  • ProductColor.ID <=> ProductColorOption.ProductColorID (1対多)

ありがとう!


更新

gaustinの以下の提案は問題を修正します(これ以上の例外はありません)が、実際の問題をターゲットにしているわけではありません。そのため、質問を「未回答」のままにします。

更新2

バグはEntity Frameworkではなく、MySQL Data Providerに関係していることがわかりました。何らかの理由で、ProductColor.IDをbyte []として処理し、盲目的にintにキャストしようとしています。これはEntity Frameworkの問題ではありません。呪い Sun Microsystems ORACLE。

役に立ちましたか?

解決

インクルードを遅延ロードしようとしましたか?

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

// ... and so on

それは参考になるかもしれません。

これも既知の問題のようです。現時点で既知の解像度があるかどうかはわかりません。ある場合、それを見つけることができません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top