views:

693

answers:

1

So basically I came up with a semi home grown solution to lazy loading by basically having generated property be private and have a public property that dealt with whether to load the generated property:

 public ChatRoom ParentRoom
 {
   get
   {
     if(!ParentRoomInnerReference.IsLoaded)
     {
        ParentRoomInnerReference.Load();
     }

     return ParentRoomInner;
   }   

   set
   {
     ParentRoomInner = value;  
   }
 }

Now this works well with queries in the class (as I have all "queries" with in a partial class extension of the generated class) by something like:

 context.Items.Where(ParentRoomInner.Id = someId).ToList();

As this method is inside the Item class, it can run this just fine where as it can't do:

 context.Items.Where(ParentRoom.Id = someId).ToList();

Since entity framework can't figure out what that proxy property is in order to generate the expression. This really becomes a problem is say I am trying to get a count of all posts (Items) for a room, and I want that method on the Room class.

 public Int32 GetPostCount()
 {
    return EntityContext.Context
           .ChatItems
           .Count(item => item.ParentRoom.ChatRoomID == ChatRoomID); //Boom
 }

For the same reason within the Item class I can't use the ParentRoom (proxy) property within the Item class, I can't use it in this query for the Room class. Entity framework just can't figure out that property.

Now question is, is there some kind of attribute I might be able to tag the ParentRoom property so that it knows that is refers to the ParentRoomInner property and therefore can know how to handle it when breaking down the expression?

The only other alternatives I can think of are having the generated property be internal and have the Room class query use the ParentRoomInner property (meh) or moving methods like the GetPostCount method to the Item class and just have the GetPostCount method on the Room class point to it. (Not terrible either but just doubles up a method)

A: 

I'm afraid the answer is "no." In .NET 3.5 there's no way to do this. .NET 4.0, of course, gives you lazy loading for real.

Putting GetPostCount on Room seems like the best fit for what you're doing.

Craig Stuntz
Sadly after an exhaustive search, I think you're right.
Programmin Tool
Here's one possible workaround:http://damieng.com/blog/2009/06/24/client-side-properties-and-any-remote-linq-provider
Craig Stuntz