EF Core (5.0.3) Is there a way to automatically load whole object graph of a lazily loaded navigation property?

54 views Asked by At

I have a navigation property that I have set up to use lazy loading (using ILazyLoader, not proxies).

public class Blog
{
    private ICollection<Post> _posts;

    public Blog()
    {
    }

    private Blog(ILazyLoader lazyLoader)
    {
        LazyLoader = lazyLoader;
    }

    private ILazyLoader LazyLoader { get; set; }

    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<Post> Posts
    {
        get => LazyLoader.Load(this, ref _posts);
        set => _posts = value;
    }
}

public class Post
{
    public Post()
    {
    }

    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public Author Author { get; set; }
    public Category Category { get; set; }
}


However, a Post object has many navigation property objects that are null after a Post is lazy loaded. So, I will either have to mark all of those to be lazy loaded (don't want), or make a big change as I don't have access to the dbContext since it's abstracted behind a repository interface.

Is there a way to automatically load the entire Post object graph on the lazy load of a Post? Could there be a way, inside dbContext, to detect the lazy load somehow and explicitly load the rest?

0

There are 0 answers