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

Ar33ss Leon
Ar33ss Leon
4,062 Points

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
168,613 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.

Ar33ss Leon
Ar33ss Leon
4,062 Points

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

Steven Parker
Steven Parker
168,613 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