Eager loading with DbContext (using Include with Lambda Expression)

Some current days, I faced with a problem with eager loading while using property name as string. But I wanna use lambda. I tried googling and found the solution.

In EF4.0,  if we wanna use it, we must install EF CTP5, but this feature has already included in EF4.1. Below are steps to use and testing result using EF Profiler

1 – Using namespace System.Data.Entity

Code Snippet
  1. using System.Data.Entity;

2 – We just use simply like that

Code Snippet
  1. public Voucher GetById(Guid id)
  2.         {
  3.             return this.Domain.Vouchers
  4.                             .Include(x => x.Account)
  5.                             .Include(x => x.Account.Address1)
  6.                             .Include(x => x.Account.Address)
  7.                             .Include(x => x.VoucherUsages)
  8.                             .FirstOrDefault(x => x.ID.Equals(id));
  9.         }

3 – The testing result

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s