Friday, 28 May 2010

DeferredLoadingEnabled options in Linq

Linq to SQL has a drawback it load data from all the available relationship of database table. So it crush or become slow that’s why if we make DeferredLoadingEnabled property false it will not load its child or parent data when we request any single row from any table. 

base.DataContext.DeferredLoadingEnabled = false;

We can specify the parent or child table name when we try to fetch any particular table data. Then it will retrieve all the related data automatically from the define table. We can do it when we make the instance of any class. 

DataLoadOptions options = new DataLoadOptions();

options.LoadWith<Builder>(m => m.SalesOffices);

options.LoadWith<SalesOffice>(m => m.ModelHomes);

options.LoadWith<ModelHome>(m => m.ModelHomeDetailImages);

options.LoadWith<SalesOffice>(m => m.SalesOfficeAds);

base.DataContext.LoadOptions = options;

In the above example we create DataLoadOptions object then assign its LoadWidth property then again assigne DataContext LoadOptions. So current DataContext instance only have above define tables relational data only.

In this way we can make faster use of Linq to SQL.

No comments:

Post a Comment