背景故事
我在一个宠物项目上遇到了一个奇怪的问题。我的程序存储了关于道具的各种信息(这只是我玩的游戏中的一个实体物品)。在这些信息中,我存储了一个缩略图和道具。当时的实体看起来是这样的:
public class PropItem
{
...
public string ThumbnailMimeType { get; set; }
public byte[] ThumbnailContent { get; set; }
public PropItem()
{
...
this.ThumbnailMimeType = string.Empty;
this.ThumbnailContent = null;
}
}然后我决定保留一个原始图像和一个缩略图,所以我创建了一个PropMediaItem并替换了PropItem上的PropItem.ThumbnnailMimeType和PropItem.ThumbnnailContent属性。这采取了以下形式:
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我基本上不能在不抛出这个错误的情况下获得返回条目的上下文。我如何物化查询并不重要。
我尝试过的
最初,我恢复到原来的结构,以确保更改是导致它的原因,并且应用程序再次开始工作。为了好玩,我决定重新实现更改,但这一次从PropItem实体中删除了新的PropMediaItem属性,它在没有额外图像实体的情况下加载得很好。
似乎当父对象与子对象具有可选关系,而子对象不存在时,它将无法加载。目前我能想到的唯一一件事就是将属性直接放到PropItem上,但这感觉就像是一种粗俗的变通方法。
我的项目是一个使用.NET 4.7.2、EntityFramework 6.4.4和MySql.Data.EntityFramework 8.0.22的ASP.NET WebForm应用程序。如有任何帮助或建议,我们将不胜感激。
发布于 2020-11-25 22:04:54
您应该检查您的计算机上安装了哪个版本的MySQL连接器/NET(我在使用MySql.Data.EntityFramework 8.0.22和MySQL连接器/NET 8.0.21时遇到了同样的问题)。
然后,阅读以下指南并仔细检查您的.config文件:https://dev.mysql.com/doc/connectors/en/connector-net-entityframework60.html
https://stackoverflow.com/questions/64689036
复制相似问题