Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repository.Find(id) and Incldude() #20

Closed
SuddenGunter opened this issue May 23, 2017 · 5 comments
Closed

Repository.Find(id) and Incldude() #20

SuddenGunter opened this issue May 23, 2017 · 5 comments
Assignees
Labels

Comments

@SuddenGunter
Copy link

How to get an entity by Id and load navigation properties values?
Only using GetPagedList?

@rigofunc
Copy link
Member

@SuddenGunter In this case, you should inject IUnitOfWork<TDbContext> instead of injecting IUnitOfWork, and then use EF Explicitly Loading feature:

var cityRepo = uow.GetRepository<City>();
var city = cityRepo.Find(5);
uow.DbContext.Entry(city).Reference(c => c.Country).Load();

var countryRepo = uow.GetRepository<Country>();
var country = countryRepo.Find(city.CountryId);
uow.DbContext.Entry(country).Collection(c => c.Cities).Load();

@rigofunc rigofunc self-assigned this May 24, 2017
@SuddenGunter
Copy link
Author

Thank you, for the answer and for the project

@DeLeneMirouze
Copy link

The problem with IUnitOfWork<> is that the Customer service need to know the real class behind DBContext (or I don't understand a point).

Wouldn't it be better to enrich the repository with a method like this:

`public async Task FindAsync(
Expression<Func<TEntity, TProperty>>[] includes = null,
params object[] keyValues)
where TProperty : class
{
TEntity found = await _dbSet.FindAsync(keyValues);
if (found == null)
{

            return null;
        }

        foreach (var include in includes)
        {
            if (include != null)
            {
                _dbContext.Entry(found).Reference(include).Load();
            }
        }

        return found;
    }`

@rigofunc rigofunc reopened this Jan 21, 2018
@rigofunc
Copy link
Member

Good idea, could you PR for this? Add some unit test.

@DeLeneMirouze
Copy link

I think there is a caveat because one may use Reference or Collection as well.
I PR another solution that implement an architecture allowing for custom repository. I thing a custom repository is a better solution for this scenario

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants