Question about the method of inclusion in the Entity infrastructure

I am building a site on asp.net MVC and I am using the repository interface to use the repository in memory and use the database.

I have all my relationships fixed, I mean, for example, when I have one contact than it has addresses, so ... when I add one address to this contact, automatically fix the relashionship for the address. contact points to contact, and I leave the virtual properties for the entity infrastructure by creating a proxy and then using the database repository.

My question starts here:

I have one such request:

return query.Where(c => c.UserID == clientId) .Include(c => c.AssignedProjects) .Select(c => new UserDetailsData<Client> { User = c, IssuesCount = c.IssuesReported.Count() }).Single(); 

which uses include. If I delete Select assignProjects, there will be projects for this client, but when I turn on Select AssignedProjects, this is null and the anonymous object is fine, but .. the user does not contain any AssignedProjects.

In memory, I can do this, but with EF, I cannot.

The last graph I want is .. A user with a client ID, having a collection of AssignedProjects with his projects and creating an anonymous object with the user (along with the collection) and the release of DataCount to show AssignedProjects, user information and the number of problems reported customer.

Does anyone know how I can solve this?

+4
source share
2 answers

You can try the following:

 return query.Where(c => c.UserID == clientId) .Select(c => new UserDetailsData<Client> { User = c, AssignedProjects = c.AssignedProjects, IssuesCount = c.IssuesReported.Count() }).Single(); 

Although you are really not interested in the AssignedProjects property of the type in which you are designing it, it will also populate the User.AssignedProjects property. It uses a β€œrange of relationships,” an EF function that automatically creates the navigation properties of objects loaded into the context.

Remember that it really depends on what you load the objects into the context. If you turn off tracking - for example, using AsNoTracking() in your request - it no longer works.

0
source

Include (impatient loading) does not work with Select (projection), because it generates a storage expression using the Entity Framework.

You can find more information. If I choose from IQueryable, then Include will be lost and you will also see work there.

I am not sure if the work around the one indicated in the list will work, although in your case your projection has several returns (and not the expected object).

Since you stated that the final schedule is what you want in addition to doing the actual calculation, why not turn this into:

 var user = query.Where(c => c.UserID == clientId) .Include(c => c.AssignedProjects) .Include(c => c.IssuesReported) .Single(); var userDetailsData = new UserDetailsData<Client>() { User = user, IssuesCount = user.IssuesReported.Count(), }; 
0
source

All Articles