问题
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
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");
}
);
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();
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
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.
Am I asking to much of EF eager loading or should this work? Could it be a MySql connector problem?
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)
回答1:
So it seems this is a bug with MySql .NET Cconnector 6.4.4 (see comments)
This has been logged with MySql
来源:https://stackoverflow.com/questions/7712620/entity-framework-many-to-many-and-eager-loading