Lazy Loading vs Eager Loading

前端 未结 8 1250
予麋鹿
予麋鹿 2020-11-27 10:36

Lazy loading in Entity Framework is the default phenomenon that happens for loading and accessing the related entities. However, eager loading is referred to the practice of

相关标签:
8条回答
  • 2020-11-27 11:10

    Lazy loading - is good when handling with pagination like on page load list of users appear which contains 10 users and as the user scrolls down the page an api call brings next 10 users.Its good when you don't want to load enitire data at once as it would take more time and would give bad user experience.

    Eager loading - is good as other people suggested when there are not much relations and fetch entire data at once in single call to database

    0 讨论(0)
  • 2020-11-27 11:11

    Eager Loading: Eager Loading helps you to load all your needed entities at once. i.e. related objects (child objects) are loaded automatically with its parent object.

    When to use:

    1. Use Eager Loading when the relations are not too much. Thus, Eager Loading is a good practice to reduce further queries on the Server.
    2. Use Eager Loading when you are sure that you will be using related entities with the main entity everywhere.

    Lazy Loading: In case of lazy loading, related objects (child objects) are not loaded automatically with its parent object until they are requested. By default LINQ supports lazy loading.

    When to use:

    1. Use Lazy Loading when you are using one-to-many collections.
    2. Use Lazy Loading when you are sure that you are not using related entities instantly.

    NOTE: Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading.

    0 讨论(0)
提交回复
热议问题