首页 > 解决方案 > 处理带有可选子项的 MySql.Data.EntityFramework 时出现异常

问题描述

背景故事

我对宠物项目有一个奇怪的问题。我的程序存储有关道具的各种信息(这只是我玩的游戏中的物理项目)。在这些信息中,我将缩略图与道具一起存储。当时的实体看起来像:

public class PropItem
{
    ...
    public string ThumbnailMimeType { get; set; }
    public byte[] ThumbnailContent { get; set; }
    public PropItem()
    {
        ...
        this.ThumbnailMimeType = string.Empty;
        this.ThumbnailContent = null;
    }
}

然后我决定我想保留一个原始图像和一个缩略图,所以我创建了一个并PropMediaItem替换了. 这采取了以下形式:PropItem.ThumbnnailMimeTypePropItem.ThumbnnailContentPropItem

public class PropItem
{
    ...
    public PropMediaItem ThumbnailImage { get; set; }
    public PropMediaItem OriginalImage { get; set; }
    public PropItem()
    {
        ...
        this.ThumbnailImage = null;
        this.OriginalImage = null;
    }
}

public class PropMediaItem
{
    ...
    public string MimeType { get; set; }
    public byte[] Content { get; set; }
    public PropMediaItem()
    {
        ...
        this.MimeType = string.Empty;
        this.Content = null;
    }
}

我更新DbContext.OnModelCreating为:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //
    // Props
    modelBuilder.Entity<PropItem>().ToTable("Props");
    modelBuilder.Entity<PropItem>().HasKey(x => x.Id);
    modelBuilder.Entity<PropItem>().HasOptional(x => x.ThumbnailImage);
    modelBuilder.Entity<PropItem>().HasOptional(x => x.OriginalImage);

    modelBuilder.Entity<PropMediaItem>().ToTable("PropMedia");
    modelBuilder.Entity<PropMediaItem>().HasKey(x => x.Id);
}

错误

但是,在进行更改并更新数据库后,每当我尝试使用数据库上下文 ( context.Props.Include("ThumbnailImage").ToList()) 获取道具时,都会收到以下错误:

[ArgumentOutOfRangeException: Non-negative number required.
Parameter name: count]
   System.Text.EncodingNLS.GetString(Byte[] bytes, Int32 index, Int32 count) +12533943
   MySql.Data.MySqlClient.NativeDriver.ReadColumnValue(Int32 index, MySqlField field, IMySqlValue valObject) +217
   MySql.Data.MySqlClient.ResultSet.ReadColumnData(Boolean outputParms) +73
   MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior) +158
   MySql.Data.MySqlClient.MySqlDataReader.Read() +116
   MySql.Data.EntityFramework.EFMySqlDataReader.Read() +13
   System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.StoreRead() +36

[EntityCommandExecutionException: An error occurred while reading from the store provider's data reader. See the inner exception for details.]
   System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.HandleReaderException(Exception e) +145
   System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.StoreRead() +49
   System.Data.Entity.Core.Common.Internal.Materialization.SimpleEnumerator.MoveNext() +50
   System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() +112
   FiveMModdingResource.Index.Page_Load(Object sender, EventArgs e) in D:\Repository\Visual Studio\website\website\index.aspx.cs:21
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +52
   System.Web.UI.Control.OnLoad(EventArgs e) +97
   System.Web.UI.Control.LoadRecursive() +61
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +693

如果不抛出此错误,我现在基本上无法获得返回项目的上下文。我如何实现查询并不重要。

我试过的

最初,我恢复到原始结构以确保更改是导致它的原因,并且应用程序再次开始工作。有趣的是,我决定重新实现更改,但这次PropMediaItem从实体中删除了新属性,PropItem并且它在没有额外图像实体的情况下加载得很好。

似乎当父母与孩子有可选关系并且孩子不存在时,它无法加载。目前我唯一能想到的就是将属性直接放在上面,PropItem但这感觉就像一个糟糕的解决方法。

我的项目是一个使用 .NET 4.7.2、EntityFramework 6.4.4 和 MySql.Data.EntityFramework 8.0.22 的 ASP.NET WebForm 应用程序。任何帮助或建议将不胜感激。

标签: c#mysqlasp.netentity-framework

解决方案


您应该检查您的机器上安装了哪个版本的 MySQL Connector/NET(我在使用 MySql.Data.EntityFramework 8.0.22 和 MySQL Connector/NET 8.0.21 时遇到了同样的问题)。

然后,阅读以下指南并仔细检查您的 .config 文件: https ://dev.mysql.com/doc/connectors/en/connector-net-entityframework60.html


推荐阅读