New Posts New Posts RSS Feed: Disable Lazy Loading
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Disable Lazy Loading

 Post Reply Post Reply
Author
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Topic: Disable Lazy Loading
    Posted: 24-May-2011 at 11:33am
Nice! Thanks Stephen.

Wokket;

Here's a link to get you started if you want to learn how to customize DF code generation.



Edited by DenisK - 24-May-2011 at 11:34am
Back to Top
stephenmcd1 View Drop Down
DevForce MVP
DevForce MVP


Joined: 27-Oct-2009
Location: Los Angeles, CA
Posts: 166
Post Options Post Options   Quote stephenmcd1 Quote  Post ReplyReply Direct Link To This Post Posted: 23-May-2011 at 1:28pm
For us, the simplest thing to do was to make the T4s generate static constructors for each entity in our model that disables the navigation properties.  There might be a better way, but it seemed to work this way.  And using static constructors, we can guarantee that the code gets executed before the particular entity's class is used.  We made a new method (shown below) and call it from WriteEntityClass (that was a convenient place for us because we already do a lot of custom code in WriteEntityClass so we were already overriding the default implementation):

     private void WriteEntityStaticConstructor(EntityOrComplexTypeWrapper entityOrComplexType)
     {
          EntityTypeWrapper entityType = entityOrComplexType as EntityTypeWrapper;
          
          //We don't use complex types so we can ignore them
          if (entityType == null)
               return;
          
          // turn off lazy loading of navigation entity relations
          WriteRegion("Static Constructor", delegate
          {
               WriteLine("static " + entityType.Name + "()");
               WriteLine("{");
               
               WriteRegion("Disable LazyLoading Navigation Relations", delegate 
               {
                    foreach (var navProperty in entityType.DeclaredNavigationProperties) 
                    {
                         WriteLine(String.Format("  PropertyMetadata.{0}.ReferenceStrategy = IbEm.EntityReferenceStrategy.NoLoad;", 
                              navProperty.Name));
                    }
               });
               
               WriteLine("}");
               WriteLine(""); 
          });
     }

Note that this will disable loading for Collection navigations and Scalar ones (1-to-many and 1-to-1) - that is the behavior we wanted...basically disable all lazy loading.

-Stephen
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 23-May-2011 at 12:00pm
Wokket;

DevForce ignores that edmx property as we generate our own object context. As of now, the only way to disable lazy loading globally is by setting the EntityReferenceStrategy or by editing the T4 templates as stephenmcd1 is suggesting. I have submitted a feature request for this so we have a way to disable it through a global EntityReferenceStrategy flag.

Stephenmcd1;

we'd love to see the code sample when you get a chance to post it. Thanks!
Back to Top
stephenmcd1 View Drop Down
DevForce MVP
DevForce MVP


Joined: 27-Oct-2009
Location: Los Angeles, CA
Posts: 166
Post Options Post Options   Quote stephenmcd1 Quote  Post ReplyReply Direct Link To This Post Posted: 23-May-2011 at 9:59am
We wanted to disable lazy-loading globally in our application as well.  We have a lot of navigation properties and are adding new ones all the time - so manually setting the ReferenceStrategy was not really an option.  Luckily, we've found editing the T4 templates to be pretty easy.  And as a bonus, it's about as safe as you can get - never have to worry about forgetting a property or anything like that.  Let me know if you want a short sample of the code we use.
Back to Top
Wokket View Drop Down
Newbie
Newbie


Joined: 17-May-2011
Posts: 17
Post Options Post Options   Quote Wokket Quote  Post ReplyReply Direct Link To This Post Posted: 20-May-2011 at 2:13pm
G'day Denis,
 
I didn't alter the EDMX properties in code, I just flipped the "Lazy Load" property in the properties window.
 
I've already started applying the default to each property in turn (that sounds wrong)....luckily this is only a fairly small project with 177 associations.
 
 
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 20-May-2011 at 12:36pm
Hi Wokket;

Thanks for letting me know. Sorry for the full inbox. It's empty now. FYI, regarding your other ticket, we believe that there might be a bug there relating to threading and PendingEntityMap. We're still trying to pin it down.

Now, returning to your specific question here, unfortunately, I don't think we have a way to globally disabled lazy loading. 

In this case, you've set the EntityReferenceStrategy.Default to .DoNotLoad, but you still have to apply it to a navigational property. For example:

Parent.PropertyMetadata.Children.ReferenceStrategy = EntityReferenceStrategy.Default;

You said that you tried disabling the LazyLoading on the .edmx properties. Is this how you've set it?



Back to Top
Wokket View Drop Down
Newbie
Newbie


Joined: 17-May-2011
Posts: 17
Post Options Post Options   Quote Wokket Quote  Post ReplyReply Direct Link To This Post Posted: 18-May-2011 at 10:08pm
G'day again,
Denis: Your PM inbox is full, but yes, that's me.
Thanks for the links, in typical form every search I've tried today has worked flawlessly... the wonders of technology.
Unfortunately despite rummaging through the docco I still haven't been able to globally disable lazy loading...
I've tried setting the default EntityReferenceStrategy as follows:

EntityReferenceStrategy

.Default = new EntityReferenceStrategy(EntityReferenceLoadStrategy.DoNotLoad, MergeStrategy.PreserveChanges);

and also disabling LazyLoading on the .edmx properties... to no avail. I'd prefer to avoid hacking at the code-genned output (via the .tt) just to make a change like this, but that's my next step unfortunately.

Back to Top
JoshO View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Location: United States
Posts: 86
Post Options Post Options   Quote JoshO Quote  Post ReplyReply Direct Link To This Post Posted: 18-May-2011 at 12:52pm
Originally posted by DenisK

Hi Wokket;


Thank you for the feedback regarding the search results. We will try to improve the search engine with this.
Strange, I tried this search myself using "lazy" and got a bunch of hits with the page linked above as the top pick. I am not sure what went wrong Wokket and I apologize for the difficulty.
Anyway, this search format also works very well using Google: site:drc.ideablade.com lazy
Just add your search term(s) after the "site:drc.ideablade.com"
Google will even try to match as many letters even though the term does not exist: lazyload will return "lazy", "lazy load", "lazy loading"; but the search engine (Lucene) used on our wiki server will not give you any hits.
You may also want to try refreshing the cached client-side scripts by hitting the "F5" button while browsing any of our DRC pages.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 18-May-2011 at 11:57am
Hi Wokket;


Thank you for the feedback regarding the search results. We will try to improve the search engine with this.
Back to Top
Wokket View Drop Down
Newbie
Newbie


Joined: 17-May-2011
Posts: 17
Post Options Post Options   Quote Wokket Quote  Post ReplyReply Direct Link To This Post Posted: 17-May-2011 at 3:07pm
G'day guys,
 
I'm running into some hassles with background asynchronous lazy-loading of properties (fired by Silverlights Binding infrastructure) causing "Collection Modfified" exceptions inside our application.
 
When I create a new entity, and attach it to the object graph being modified, I'm currently having to set each and every referenced entity to a new entity (via EntityManager.CreateEntity<T>()) in order to avoid a lazy-load lookup.
 
I've found info from Ward here (http://forums.silverlight.net/forums/t/178991.aspx) indicating lazyloading can be disabled, and a few forum posts indicating it can be controlled, however no relevant search results here, and no results in the documention wiki for lazy, lazyload, lazyloading etc.
 
Can anyone provide a link to some lazy-load docco, or provide feedback on the preferred way of constructing an entity graph like this?
 
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down