Entity Framework references aren't automatically loaded.

ado.net c# entity-framework


I have an object in the ADO.Net Entity Framework that is referenced by four other objects. Two of the references automatically load (as anticipated) when I query them, but two of them always return null for some reason.

Strangely enough, the references load perfectly when I manually ask them to.

As an illustration

if (account.HoldingEntity == null && 
    account.HoldingEntityReference.EntityKey != null) {

    account.HoldingEntity = account.HoldingEntityReference.Value;

When I first review theHoldingEntity Despite the fact that it is always null, the Load will successfully return the HoldingEntity.

Any hints?


6/23/2009 11:08:44 AM

Accepted Answer

You must define the entities you wish to automatically load using ADO.NET Entities.Include , as in

Dim entity = (From e in db.Entities.Include("SubEntity"))
6/23/2009 11:20:58 AM

Popular Answer

As others have said, you should.Include() in v1 to prevent having to dial.Load()

4.0 will allow you to setDeferredLoadingEnable in yourObjectContext (I believe we should change this name to one that is more fitting.LazyLoadingEnabled prior to Beta 2).

As for why you already have two laden relationships. The result of anything called Relationship Fix-up is probably that.

When two related things are placed in the same context, their relationships are automatically fixed to point to one another. Therefore, when you run the query, even though you didn't call, 2 of your relationships will be loaded if (as I believe) 2 of the 4 entities are already in your context..Include() or .Load() .

Hope this is useful.

Cheers Alex

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow