Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL: On load processing of lazy loaded associations

If I have an object that lazy loads an association with very large objects, is there a way I can do processing at the time the lazy load occurs? I thought I could use AssociateWith or LoadWith from DataLoadOptions, but there are very, very specific restrictions on what you can do in those. Basically I need to be notified when an EntitySet<> decides it's time to load the associated object, so I can catch that event and do some processing on the loaded object. I don't want to simply walk through the EntitySet when I load the parent object, because that will force all the lazy loaded items to load (defeating the purpose of lazy loading entirely).

like image 987
Matt Holmes Avatar asked Apr 30 '10 17:04

Matt Holmes


3 Answers

Subscribe to the ListChanged Event

EntitySet<T> exposes an event called ListChanged which you can use to detect if an item is being added. Evaluate the ListChangedType property of the ListChangedEventArgs. Here's a link to the values available in the ListChangedType enum.

There is no danger of forcing the load to execute as long as you avoid requesting the enumerator.

http://msdn.microsoft.com/en-us/library/system.componentmodel.listchangedtype.aspx

like image 143
randolphcabral Avatar answered Oct 05 '22 23:10

randolphcabral


I don't see any extensibility points for this available; the only thing I can see is in the FK entity there is a Created method on each individual object that gets fired from the constructor...

So the constructor calls created, and personally, I'm not 100% sure that the entity set loading creates each individual object at that time, and fires the event...

HTH.

like image 42
Brian Mains Avatar answered Oct 05 '22 23:10

Brian Mains


There are a bunch of built in extensibility methods to the datacontext and data classes generated by Linq2SQL.

http://msdn.microsoft.com/en-us/library/bb882671.aspx

http://csainty.blogspot.com/2008/01/linq-to-sql-extending-data-classes.html

Either of these may be able to serve the purpose you need.

like image 26
Adam Avatar answered Oct 05 '22 23:10

Adam