DEV Community

Supraja Tangella
Supraja Tangella

Posted on

๐—˜๐—ฎ๐—ด๐—ฒ๐—ฟ ๐˜ƒ๐˜€ ๐—Ÿ๐—ฎ๐˜‡๐˜† ๐—Ÿ๐—ผ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด

In ๐—˜๐—ป๐˜๐—ถ๐˜๐˜† ๐—™๐—ฟ๐—ฎ๐—บ๐—ฒ๐˜„๐—ผ๐—ฟ๐—ธ ๐—–๐—ผ๐—ฟ๐—ฒ, how and when related data is fetched matters โ€” thatโ€™s where ๐—˜๐—ฎ๐—ด๐—ฒ๐—ฟ ๐—Ÿ๐—ผ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด and ๐—Ÿ๐—ฎ๐˜‡๐˜† ๐—Ÿ๐—ผ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด come in.

๐—˜๐—ฎ๐—ด๐—ฒ๐—ฟ ๐—Ÿ๐—ผ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด means related data is loaded immediately with the main entity using .Include().

๐Ÿ‘‰ Example:

var products = _context.Products.Include(p => p.Category).ToList();

Itโ€™s like ordering a meal combo โ€” you get the burger, fries, and drink together in one go.

๐—Ÿ๐—ฎ๐˜‡๐˜† ๐—Ÿ๐—ผ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด means related data is loaded only when accessed.

๐Ÿ‘‰ Example:

Console.WriteLine(product.Category.Name);

Itโ€™s like ordering dessert only if you still feel hungry after your meal.

Choosing the right approach improves performance, database efficiency, and overall application responsiveness.

Which approach do you prefer in your real-world projects โ€” Eager Loading for speed or Lazy Loading for flexibility?

Top comments (0)