【发布时间】:2021-02-17 16:14:51
【问题描述】:
背景故事
我在一个宠物项目上遇到了一个奇怪的问题。我的程序存储有关道具的各种信息(这只是我玩的游戏中的物理项目)。在这些信息中,我将缩略图与道具一起存储。当时的实体看起来像:
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 应用程序。任何帮助或建议将不胜感激。
【问题讨论】:
标签: c# mysql asp.net entity-framework