2

This is my first attempt at understanding Linq to Entity coming from Net Tiers. Struggling with it but not understanding some of the logic behind it... for instance. When do I use:

Entity.EntitySet.Load()

and

context.Entity.Include("EntitySet").SingleOrDefault()

Also why does Include take a string and not an enum or sorts?

Rob
  • 3,074
  • 4
  • 31
  • 32

2 Answers2

1

In EF you have the concepts of Lazy loading and Eager loading.

  • Lazy loading means you load the data the moment you need it. This is done trough the Load() method call.
  • Eager loading means you already know upfront that you will need some data so you load it in the initial query trough Include(string).

Al tough Include takes a string this doesn't mean you can't extend this!

T4 is a nice thing. In a project I've worked on we created an EntityProperty class that contained static properties for all the Navigational properties on an entity. This way you would at least get a compile error if a property name changes.

If you really want to go one step further you could build a Include method that takes a Lambda and then rearrange the Expression tree before executing it to the QueryProvider. Then you would have nice static typing.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
  • Just read more on Lazy Loading and Eager loading. The problem I have now, (perhaps another question), is that my entity object, now detached, cannot read Entity.EntitySets after using Include. Count is zero where as initial load showed 10. Do I have to reload every time I want the EntitySet values? (Hope this makes sense). – Rob Oct 27 '11 at 06:01
  • Could you update your starting post with a code example of what you're doing? – Wouter de Kort Oct 27 '11 at 06:03
  • I think I have made a mistake in processing the webpage. I'm loading the EntitySets when I don't need to in the Page_Load. Long story but I think I need to raise another question in stackoverflow about the best way to check the number of entitysets with an already detached entity. – Rob Oct 27 '11 at 06:11
0

Include is typically used for loading related tables. Load explicitly loads the requested entity.

String is used because MS chose to - not aware of any real limitation. Sort of how T4 solves the issue of string actions in MVC - maybe they will get around to improving/extending it to support stronger typing.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173