1

Using ASP.NET 4.5 and EF 6, I've put together a multilevel data model that looks like this:

Organization, which has an ICollection of:

_____Workspaces, which has an ICollection of:

__________Projects, which has an ICollection of:

_______________Cards

When I ask the database for a list of organizations using the following code, I get a giant multilevel nested response all the objects in the database.

var orgs = await (from o in db.Organizations select o).ToListAsync();

How do I specify that I just want the top level to be returned? (or any other specific depth of search?)

I'm sure this is easy, but I'm new to SQL C# world and don't know the proper language to find useful Google or SO answers...


Update: kienct89 gets the win!

"Lazy Loading" is the right term, and here's some good info about it.

waffles
  • 1,386
  • 12
  • 18
  • can you most more details about your model? it's possible you need to address lady loading. Look at lazy loading in [this tutorial](http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application) – devlin carnate Jan 04 '16 at 03:04

2 Answers2

2

You're looking for Eager loading vs Lazy loading in entity framework

Lazy Loading: data will be loaded automatically when you execute the query (serialize the object, cast to enumerable, list, etc)

Eager Loading: data will be loaded manually ONLY IF you want to (by using Include() function)

If you want to turn off Lazy Loading you can use the block code below

Configuration.LazyLoadingEnabled = false; // in the db context class
Kien Chu
  • 4,735
  • 1
  • 17
  • 31
1

If you do not wish to globally disable lazy loading (which Configuration.LazyLoadingEnabled does), you can target specific properties by omitting the virtual key word in the declaration.

Community
  • 1
  • 1
devlin carnate
  • 8,309
  • 7
  • 48
  • 82