LINQ to SQL Instantiation
page 7 of 8
by Brian Mains
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 43829/ 81

Deferred vs. Immediate Loading

By default, LINQ objects are loaded in a deferred nature; this means that any child objects of a parent are not loaded until they are accessed. So for the Customer object, once the Orders collection is accessed, these orders are loaded at this point, rather than loading them all at the same time. This means another database query pulls back the orders data, involving two hits to the database.

However, it is possible that loading the parent data also loads the child data immediately, using the following setup.

Listing 10

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Employee>(i => i.EmployeePositions);
_context.LoadOptions = options;

The DataContext class has a LoadOptions property that specifies any children to load for a given parent.  So, when loading employee information, any related records in EmployeePositions (a join table designed for a many-to-many join situation) are also loaded. This prevents a second query for any relationship data. You must define load options before querying against the context.

You may wonder whether that is better; really, it depends on your situation. I cannot say that in every situation, immediate loading is a better approach. Sometimes, you do not need all of the related data, so a deferred approach is better. However, other times, you will use all of the employee and related position records, and so immediate loading would help reduce the number of calls to the database.

Ideally, it is the number of database calls that immediate loading is trying to reduce, and whether that really reduces the number of calls depends on the type of application, the size of the data coming back, the relationships between the data, etc.

View Entire Article

User Comments

Title: Another way to do this...?   
Name: Funka!
Date: 2009-08-14 5:17:13 PM
Another possibility it seems is to create a partial class and partial method "OnCreated" ?
For example:
public partial class Order
partial void OnCreated()
_OrderKey = Guid.NewGuid();
_CreatedDate = DateTime.Now;
This way, you don't need to keep remembering to do this every time you instantiate an object.

It should also be noted that you avoid your specific error (but not the general need for setting default values) by setting the LTS entity property's "Auto Generated Value" to true --- LTS will know not to supply a value to these columns when inserting, so you can just let SQL server fill in these defaults.

Title: Great Article !!!   
Name: Jash
Date: 2009-04-13 3:24:14 PM
Thank you Brian really useful article.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2022  |  Page Processed at 2022-08-15 7:01:26 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search