DeferredLoadingEnabled - how does it work?

3.1k views Asked by At

I've several tables including the 'combining' in many-to-many relationship. I want to make now Insert operation.

I heard about DeferredLoadingEnabled in linq2sql and I know it's connected with this topic, but I don't quite know how it works?

For example

[ClientTable]: ClientId, ClientName, Phone, Age
[CityTable]: CityID, CityName, Country
[ClientCityTable]: ClientCityID, ClientID, CityID

Could someone show it at this simple example or provide some nice link?

1

There are 1 answers

0
Pleun On BEST ANSWER

It has absolutely nothing to do with inserting.

Deffered loading means that Linq-2-sql will only go the database as soon as you actually use the linked tables.

In your example, if you load a [ClientCity] the Cityname will not be loaded untill you actually use it.

So if you do:

var thisOne = db.ClientCities.Single(a=>a.id == 1);

The CityTable entity will not be loaded until you do something like:

string x = thisOne.CityTable.Cityname

Only at that time, the CityTable is loaded from the db.

This can be great if you only occassionaly need the city but it can lead to N+1 queries as soon as you loop to all your CityTables

There are plenty of links around. E.g: http://msdn.microsoft.com/en-us/library/bb399393.aspx

or http://www.west-wind.com/weblog/posts/2009/Oct/12/LINQ-to-SQL-Lazy-Loading-and-Prefetching