Include metodu ne için kullanılır?

Kod örneğimiz şekildeki gibi

1
2
3
4
5
public ViewResult Index()
{
var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist);
return View(albums.ToList());
}


LOADING RELATED OBJECTS

The Include method calls that you see in the Index action tell the EF to use an eager loading strategy in loading an album’s associated genre and artist information. An eager loading strategy attempts to load all data using a single query. The alternative (and default) strategy for the EF is a lazy loading strategy. With lazy loading, EF loads only the data for the primary object in the LINQ query (the album), and leaves the Genre and Artist properties unpopulated:


1
var albums = db.Albums;
Lazy loading brings in the related data on an as-needed basis, meaning when something touches the Genre or Artist property of an Album, EF loads the data by sending an additional query to the database. Unfortunately, when dealing with a list of album information, a lazy loading strategy can force the framework to send an additional query to the database for each album in the list. For a list of 100 albums, lazy loading all the artist data requires 101 total queries. The scenario just described is known as the N+1 problem (because the framework executes 101 total queries to bring back 100 populated objects), and is a common problem to face when using an object-relational mapping framework. Lazy loading is convenient, but potentially expensive.

You can think of Include as an optimization to reduce the number of queries needed in building the complete model. To read more about lazy loading see “Loading Related Objects” on MSDN at http://msdn.microsoft.com/library/bb896272.aspx.

Yorumlar

Bu blogdaki popüler yayınlar

22.06.2020 - 26.06.2020 arası işler

Asp.net RestSharp ile data post etmek

List Box Item içindeki elemanları aşağı veya yukarı taşımak