Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - lazy loading or additional async/await query method?

I have these Domain Models

public class Topic
{
    public int TopicId { get; set; }

    public virtual ICollection<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }

    public int? TopicId { get; set; }
    public virtual Topic Topic { get; set; }
}

For example I would like to impliment method TestAsync, there I want to work with Topic object and related Posts objects.

Topic model I can get using async method and topicId as param.

public async Task<bool> TestAsync(int topicId)
{
    var topic = await topicService.GetByIdAsync(topicId);

    // posts ...
}

And I have two ways, how to get related posts. But, what the difference if I will use LazyLoading or just another async query?

// Example: 1 (LazyLoading)
var posts = topic.Posts;

// OR Example: 2 (Async method)
var posts = await postService.GetAllByTopicIdAsync(topicId);

So, I think that Example:1 will works synchronously, and also that I lose all the advantages of async/await code. But Example: 2 makes me think, that may be I dont know all charms of Lazy Loading:) Could anyone clarify what solution I should use and why? Thanks:)

like image 277
Vladimirs Gorkins Avatar asked Jul 31 '17 16:07

Vladimirs Gorkins


People also ask

Does Entity Framework support lazy loading?

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

Why do we use lazy loading in Entity Framework?

Lazy loading is the process whereby an entity or collection of entities is automatically loaded from the database the first time that a property referring to the entity/entities is accessed. Lazy loading means delaying the loading of related data, until you specifically request for it.

What is the difference between eager and lazy loading in Entity Framework?

While lazy loading delays the initialization of a resource, eager loading initializes or loads a resource as soon as the code is executed. Eager loading also involves pre-loading related entities referenced by a resource.

How do I turn off lazy loading in Entity Framework?

We can disable lazy loading for a particular entity or a context. To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false.


1 Answers

Lazy loading is always synchronous, which is unfortunate. EF Core, for example, with its async-first mentality, does not (yet) support lazy loading.

Other options are to either do a join (eager loading) as Peter suggested, which asynchronously performs a single query; or to do an explicit second asynchronous query. Which one you'd choose comes down to how your model is normally used.

Personally, I would choose to do the eager loading if the models are always used together, and do multiple asynchronous queries otherwise. I do not use lazy loading myself, though nothing would prevent it from working.

like image 72
Stephen Cleary Avatar answered Sep 28 '22 17:09

Stephen Cleary