Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Entity Framework with ASP.NET MVC Entity Framework and ASP.NET MVC Solution: Finishing the Comic Book Artists Controller

I am obligated to use Lazy loading?

I understood well to the instructor about eager and lazy loadings, for example in eager loads you need to add a .include() and in lazy loads add a virtual to any navgation property but... for example I do not always use first code to code my database using EF I always use sql server to model my databases and import via EF to my project but there is a problem. Imported databases from Entity frameworks are usually set to virtual which means they are in lazy loading mode. My question is, if they are set virtual by default am I obligated to use lazy loading? or did I misunderstood about the differences between them?

1 Answer

Steven Parker
Steven Parker
229,732 Points

There might be some some properties you can set to change loading behavior, but a really simple way to force immediate loading is to chain a ".ToList()" onto your query.

give me an example, I did not get you :( or can you be more specific please .

Steven Parker
Steven Parker
229,732 Points
var items = context.Products.Where(x => x.Model == "Widget")
                            .Select(x => x.Name)  // this would be "lazy" ...
                            .ToList();            // but adding this makes it immediate

got you!