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

Query using Include problem #7610

Closed
sonphnt opened this issue Feb 14, 2017 · 4 comments
Closed

Query using Include problem #7610

sonphnt opened this issue Feb 14, 2017 · 4 comments

Comments

@sonphnt
Copy link

sonphnt commented Feb 14, 2017

Hi Everyone,

I have 2 classes like:
Team and Member

I am using Scaffold-DbContext as Database first. It generates 2 classes with

public partial class Member
{
        public virtual Team Team {get;set;}
}

public partial class Team
{
        public virtual ICollection Member Member {get;set;}
}

And I want to get all member in database with Team object to get some information about the team, so I use this query

dbContext.Member.Include(x=>x.Team).ToList();

But the problem when I debug. It also load Members list inside Team entity as well. There are a lot of data.
The object looks like this

[
{
     Name: "John",
     Team: {
          Name: "Team 1",
          Member: [] **// I want to avoid and ignore loading this list**
     }
}
]

How to avoid this and using correctly?

Thank you very much

@ajcvickers
Copy link
Contributor

@sonphnt The include tells EF that for each Member that is loaded the related Team should also be loaded. When that Team is loaded the already loaded Members are added to the collection on Team. In other words, EF isn't loading any more data, it is simply fixing up the references between already loaded data.

If you are having issues with JSON serialization, then you might want to look at this: #4646 and this #7564

@sonphnt
Copy link
Author

sonphnt commented Feb 15, 2017

Hi @ajcvickers

Thanks for your reply but is there any settings or extensions to avoid "When that Team is loaded the already loaded Members are added to the collection on Team"

Because Parsing that collection to Json will include that Members array on Team that will generate a huge Json file unless I have to do manually code to get rid of that collection before returning to client.

Thanks

@NageshAndani
Copy link

NageshAndani commented Aug 19, 2018

@sonphnt , I too has found this behavior resulting in a huge JSON object. As the issue is "closed-by-design", I chose to fix it myself by manually setting the Member to null for each member.

var members = dbContext.Member.Include(x=>x.Team).ToList();
foreach (var member in members)
{
        member.Team.Member = null;
        break; // Because, setting first member object's Team.Member to null, would update for each member
}

This would result in:

[
{
     Name: "John",
     Team: {
          Name: "Team 1",
          Member: null
     }
}
]

@sonphnt
Copy link
Author

sonphnt commented Aug 19, 2018

Yes. To avoid returning a huge json to client. You should not return entity model like that. You need to cast or convert them to view model or using automapper way.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants