SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • LINQ (4)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box
ASP.NET Hosting with MS SQL 2008 – Click Here!
Monday, October 27, 2008 : 7:19 PM - (0 comments)

EntityFramework "Include" Equivalent in LINQ to SQL

Recently at my job, we switched from using DataSets over Web Services to using the EntityFramework over WCF. Within about two days, we switched off of using EF, and went to LINQ to SQL (which is my love really). The point of this post is not to put down EF, but is rather to show how to achieve one of the pieces of functionality in there that I liked... but with LINQ to SQL instead.

EntityFramework's "Include" Method

Because the EntityFramework doesn't support lazy loading, you have to use the "Include" method to retrieve related data in your query. What I mean by this is that if you have a table called "Customers", and a table called "Orders" (which are linked together), this query will not populate the "Orders" data:

// Using the Entity Framework
MyEntities context = new MyEntities();

var orders = context.Customers
   .First().Orders;

but this code will:

// Using the Entity Framework
MyEntities context = new MyEntities();

var orders = context.Customers
   .Include("Orders")
   .First().Orders;

This is not new information, but what was new to me is that LINQ to SQL also supports this type of functionality (plus the implementation is better in my opinion).

EF's "Include" Equivalent in LINQ to SQL

You may be wondering, "Since LINQ to SQL supports lazy loading, why would you want to pre-download related data?" And the answer is quite simple: performance and distribution. If you know for a fact that you are going to use the "Orders" data when you pull your customer object, then it's much faster to hit SQL only once and download all the data, then loop through it in memory.

Also (as is in my case), if you are planning on sending the object over a WCF connection (or a typical web-service), then you'll need to have all of the data in the object graph before sending it. Once the object is sent to the client, that's it!

So, how do we achieve the same '.Include("Orders")' functionality? Here's the code:

// Using L2S
MyDataContext context = new MyDataContext();

// Create "options" for this L2S context.

DataLoadOptions options = new DataLoadOptions();

// Tell the options that we plan on using the Orders.

options.LoadWith<Customer>(item => item.Orders);

// Set the options on the context.

context.LoadOptions = options;

// There we go!

var orders = context.Customers
   .First().Orders;

You have to set your "LoadWith" options *before* you assign the "LoadOptions" property on the context. If you assign the property first, and then call "LoadWith", you'll get the following error: "LoadWith is not allowed after freeze or attach to DataContext."

Conclusion

So to wrap up this short blog post in an even shorter few sentenses, I'll conclude by saying the following: The EntityFramework doesn't support lazy loading. Instead, you have to specifically load relational properties with the "Include" method. LINQ to SQL does support deferred loading, but you can choose to load relational properties at the same time using the "LoadWith" method.

Wednesday, October 1, 2008 : 12:01 PM - (0 comments)

Plans to the Death Star... Or an Auto-Generated EDM?

I figured I have to post this really quickly... I just used the Entity Framework wizard to generate an EDM for the company I currently work for. For now, we're just doing a 1-to-1 of the database... (205 tables, 655 stored procedures, 2 views and 40 functions).

The generated designer file (in C#) is 52,670 lines... yikes.

Auto Generated EDM for Large Database
Developer / Architect / Author

Blog Archives

  • November 2008 - (1)
  • October 2008 - (2)
  • September 2008 - (2)
  • August 2008 - (3)
  • July 2008 - (1)
  • June 2008 - (3)
  • May 2008 - (2)
  • April 2008 - (2)
  • March 2008 - (4)
  • February 2008 - (2)
  • December 2007 - (2)
  • November 2007 - (1)
  • October 2007 - (4)
  • September 2007 - (9)
  • August 2007 - (7)

Related Ads

SingingEels.com as of Jan 06 2009 - 01:59:50 AM - (0.0468738)