I have a specific query in my code which needs to eager load all related entities (both ->1 FKs and ->N FKs) because the context will be disposed right after that.
I made a generic "Query" method that takes params Expression<Func<MyItem, object>>[] includes
and then internally chains them. That part works fine.
The query looks like this:
var item = facade.Query<MyItem>(
c => c.Childs.Select(x => x.Parent),
c => c.Childs.Select(x => x.SubChild1),
c => c.Childs.Select(x => x.SubChildNotWorking),
c => c.Childs.Select(x => x.SubChild2),
c => c.Childs.Select(x => x.SubChild3),
c => c.Childs.Select(x => x.SubChildrens)
).FirstOrDefault(c => c.Name == name);
The mapping for the not working property (placed in the configuration of SubChildNotWorking):
this.HasMany(scnw => scnw.Childs).WithOptional(c => c.SubChildNotWorking).HasForeignKey(c => c.MyForeignKey);
Of all the includes, only the SubChildNotWorking
doesn't actually work. When inspecting with the debugger the returned object, I see the proxies on all the properties. Opening the proxies gives me the correct data for all other relations, and a "The objectcontext has already been disposed exception" for the SubChildNotWorking
property.
The only difference I was able to spot is that the SubChildNotWorking
is a nullable FK (with nullable column on the DB and WithOptional configuration in the dbcontext) while all the others are non nullable FKs configured with WithRequired.
The database also is a legacy DB not created with Code First and not following its conventions, I just made the mappings in the DbContext. Everything else works fine.
I am trying to figure out if eager loading doesn't work on nullable FKs but I couldn't find any documentation about that.
Is that a bug, or intended behavior? But most of all, how do I solve this?
Thanks.