OT: Linq lazy loading not working?

OT: Linq lazy loading not working?

Old forum URL: forums.lhotka.net/forums/t/4499.aspx

ajj3085 posted on Friday, March 14, 2008


I have an Association on my table; the association doesn't seem to
load.  Here's the association:

private EntityRef<Shared.Employee> createdBy =
default( EntityRef<Shared.Employee> );

[Association( ThisKey = "CreatedById", IsForeignKey = true )]
public Shared.Employee CreatedBy {
   get { return createdBy.Entity; }
   set { createdBy.Entity = value; }


I try to access the association like this:

createdByName = myClass.CreatedBy.LogonName; // Shouldn't this cause
CreatedBy to load?

Instead I get a NullReferenceException.  CreatedById is a valid value
and a record does exist, but Linq never seems to even bother executing
the select to load the Shared.Employee instance.

Any ideas?


Skafa replied on Friday, March 14, 2008

I haven't tried this, but when looking at the examples in de documentation (http://msdn2.microsoft.com/en-us/library/bb425822.aspx)
the EntityRef field isn't initialized with something...

so, try removing default(EntityRef<...

ajj3085 replied on Friday, March 14, 2008

I'm trying to figure this out by creating entites via the designer instead of hand coding the needed additions.  That default( EntityRef<entity> ) is in the designer generated code as well. I'm at a point now where everything is almost identical to the designer code, expect I don't tell Linq the private backing field. maybe that has something to do with it?  I'll post back..

Skafa replied on Friday, March 14, 2008

I don't know either then..

When you specify a private backing field. Dlinq doesn't use the getter/setter for that property. So if you have custom business logic in your setter/getters, you should'nt specify a backing field.

ajj3085 replied on Friday, March 14, 2008


Ok.  When you have an association, you MUST specify the Stroage for the association, or it just silently fails.

Hope that helps someone else..


Copyright (c) Marimer LLC