Base solution for your next web application
Open Closed

EnsureLoadedAsync how to use and extensions #3267


User avatar
0
customreg created

Hello,

What is the recommended way to load related data using the IRepository pattern and EF Core?

EF Core has a way to load related navigation properties, and to continue down the chain with .ThenInclude calls.

I see EnsureLoadedAsync was added recently, but it does not accomplish these two things I need:

  1. EnsureLoadedAsync only works when loading IEnumerable properties. What if I need to load a single related property?
  2. I need a way to go more than one level past the IRepository for an entity.

Any tips?


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I haven't tried it but it seems like you can use that syntax in IRepository, have you tried it in AspNet Zero ? After calling _myRepository.GetAll(), you can use Include and ThenInclude.

    Thanks.

  • User Avatar
    0
    customreg created

    Yes thanks, I've been using that method you suggested.

    This basically reduces IRepository to an IQueryable using EF Core.

    I gotta say, after a few projects trying to put a repository abstraction on top of EF, I'm not a fan of this abstraction... Could someone please explain why it is necessary in this framework?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Repository pattern is a well known pattern and it is an abstraction for data access. It has many benefits like unit testing and it allows to use different data access providers without changing most of your code. You can find more info about it's benefits on the web.

    Thanks.