views:

83

answers:

2

Hi,

When using Linq2sql everything automagically works. My experience is that going with the flow is not always the best solution and to understand how something internally works is better so you use the technique optimally.

So, my question is about linq2sql.

If I do a query and get some database objects, or I create a new one, somehow the linqcontext object keeps references to these objects. If something changes in one of the objects, the context object 'knows' what has changed and needs updating.

If my references to the object are set to null, does this mean that the context object also removes it's link to this object? Or is the context object slowly getting filled with tons of references, and keeping my database objects from garbage collecting?

If not, how does this work??

Also, is it not very slow for the database object to always go through the entire list to see what changed and to update it?

Any insight in how this works would be excellent!

thanks

+2  A: 

yes, the context keeps references of the loaded objects. That's one of the reasons why it isn't meant to be used with a single instance shared accross the different requests.

It keeps lists for the inserts/deletes. I am not sure if it captures update adding those to a list, or it loops at the end. But, u shouldn't be loading large sets of data at a time, because that alone would be a bigger hit to performance than any last check it might do on the list.

eglasius
what do you mean with share across different requests? Shoudl you create a new context for every query?
Toad
@reinier that's on asp.net (which might not be your case), if u keep the same context instance for different requests u are keeping all the tracked objects in there. Besides performance, consider what if some process was doing some changes in some db objects and because of some validation u didn't submit those changes ... then in another process, u do some unrelated changes and call submit changes in the context --- u would be committed those other changes u didn't intended to.
eglasius
Yes, it is preferred to keep your datacontext lifecycle as short as possible.
Marcel Gosselin
freedy,marcel: never knew the context should be short lived
Toad
y http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx - "A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations". That hint is even shorter than the typical use I give it, in line with Marcel's comment.
eglasius
+2  A: 

The DataContext registers to your objects PropertyChanged event to know when it is modified. At this point it clones the original object and keeps it to compare the 2 objects together later when you do your SubmitChanges().

If my references to the object are set to null, does this mean that the context object also removes it's link to this object?

Edit: No. Sorry for my original answer I had misinterpreted what you had written. In that case the data context still has a reference to both object but will remove the relationship with those 2 objects on next SubmitChanges().

Be careful though. If you created your own objects instead of using the ones generated from the .dbml, the "magic" that the datacontext performs might not work properly.

Marcel Gosselin
with creating objects i meant the ones the dbml created from my tables
Toad
@Marcel afaik it doesn't removes the unreferenced objects when u set your own references of the object to null.
eglasius
Thanks Freddy, I edited my answer
Marcel Gosselin
@reinier since you used the objects created from VisualStudio, all the updates should work fine. For insertions and deletions, you willl need to use `InsertOnSubmit()` and `DeleteonSubmit()`.
Marcel Gosselin