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:
- EnsureLoadedAsync only works when loading IEnumerable properties. What if I need to load a single related property?
- I need a way to go more than one level past the IRepository for an entity.
Any tips?
3 Answer(s)
-
0
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.
-
0
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?
-
0
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.