实体框架多对多且加载量大 [英] Entity Framework Many to Many and Eager Loading

查看:103
本文介绍了实体框架多对多且加载量大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有MySql的Entity Framework 4.1,并且急于加载多对多关系.受影响的表的简化版本如下:

I'm using Entity Framework 4.1 with MySql and having a problem with the eager loading of a many to many relationship. A simplified version of the affected tables looks like:

CabinCategory - CabinCategoryId
CabinGrade - CabinGradeId, CabinCategoryId
Deck - DeckId
DeckCabinGrades - DeckId, CabinGradeId <--- Join table

我最终试图访问CabinCategory.CabinGrade.Deck

I'm ultimately trying to access CabinCategory.CabinGrade.Deck

我认为我已经成功地使用

I think I've managed to map this successfully using

        modelBuilder.Entity<CabinGrade>()
            .HasMany(c => c.Decks)
            .WithMany(d => d.CabinGrades)
            .Map(m =>
                {
                    m.MapLeftKey("CabinGradeId");
                    m.MapRightKey("DeckId");
                    m.ToTable("DeckCabinGrades");
                }
            );

如果我渴望加载CabinGrades,但让Decks延迟加载,那一切都很好.但是,如果我尝试用以下方式加载CabinGrades和Decks:

If I eager load CabinGrades but let Decks lazy load everything is great. However, if I attempt to eager load CabinGrades and Decks with

this.Context.CabinCategories.Include("CabinGrades").Include("CabinGrades.Decks").ToList();

我得到了null引用异常和以下堆栈跟踪

I get a null refference exception and the following stack trace

[NullReferenceException: Object reference not set to an instance of an object.]
System.Data.Objects.DataClasses.RelatedEnd.GetOtherEndOfRelationship(IEntityWrapper wrappedEntity) +57
System.Data.Objects.ObjectStateManager.AddEntityToCollectionOrReference(MergeOption mergeOption, IEntityWrapper wrappedSource, AssociationEndMember sourceMember, IEntityWrapper wrappedTarget, AssociationEndMember targetMember, Boolean setIsLoaded, Boolean relationshipAlreadyExists, Boolean inKeyEntryPromotion) +683
System.Data.Objects.ObjectStateManager.UpdateRelationships(ObjectContext context, MergeOption mergeOption, AssociationSet associationSet, AssociationEndMember sourceMember, EntityKey sourceKey, IEntityWrapper wrappedSource, AssociationEndMember targetMember, IList targets, Boolean setIsLoaded) +797
System.Data.Common.Internal.Materialization.Shaper.FullSpanAction(IEntityWrapper wrappedSource, IList`1 spannedEntities, AssociationEndMember targetMember) +367
System.Data.Common.Internal.Materialization.<>c__DisplayClass2`2.<HandleFullSpanCollection>b__0(Shaper state, List`1 spannedEntities) +38
System.Data.Common.Internal.Materialization.Coordinator`1.ResetCollection(Shaper shaper) +193
System.Data.Common.Internal.Materialization.RowNestedResultEnumerator.MoveNext() +251
System.Data.Common.Internal.Materialization.ObjectQueryNestedEnumerator.TryReadToNextElement() +31
System.Data.Common.Internal.Materialization.ObjectQueryNestedEnumerator.ReadElement() +40
System.Data.Common.Internal.Materialization.ObjectQueryNestedEnumerator.MoveNext() +51
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +471
System.Linq.Enumerable.ToList(IEnumerable`1 source) +79

当我检查MySql查询日志时,它实际上生成了一个查询,该查询可以正常工作并获取Decks.好像无法将结果转换回对象.

When I check out the MySql query log it's actually generated a query that works fine and gets the Decks. It's as though it can't convert the results back into objects.

我是否要求EF急切加载,还是应该工作?可能是MySql连接器问题吗?

Am I asking to much of EF eager loading or should this work? Could it be a MySql connector problem?

编辑:当它只有一个级别"深时(CabinGrade.Decks),它可以正常工作.当深度为两个级别"时,似乎会出现问题(CabinCategory.CabinGrade.Decks)

EDIT: It works fine when it's only one 'level' deep (CabinGrade.Decks). The problem seems to occur when it's two 'levels' deep (CabinCategory.CabinGrade.Decks)

推荐答案

因此,这似乎是MySql .NET Cconnector 6.4.4的错误(请参见注释)

So it seems this is a bug with MySql .NET Cconnector 6.4.4 (see comments)

已使用MySql记录

这篇关于实体框架多对多且加载量大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆