We're pulling data for Users using Entity Framework code similar to what is below:
using (DataContext db = new DataContext())
{
var users = db.Users.Include("Roles.Role");
}
This effectively gets the Roles and Logins that we're looking for. However, because Role also has Users on it for the users in that role, it's also being populated. That means we get a result similar to what's below:
[{
"roles" : [{
"userId" : 1,
"roleId" : 1,
"role" : {
"users" : [{
"userId" : 4,
"user" : {
"roles" : [],
"firstName" : "Jill",
"lastName" : "Doe",
"id" : 4
},
"roleId" : 1
}, {
"userId" : 1,
"user" : {
"roles" : [],
"firstName" : "John",
"lastName" : "Doe",
"id" : 1
},
"roleId" : 1
}
],
"id" : 1,
"name" : "Administrator"
}
}
],
"firstName" : "John"
"lastName" : "Doe"
"id" : 1
}]
We're not explicitly requesting that the Users on the Role be loaded, but it seems like it's loading them because it already knows who they are because it loaded the users as part of the main request.
We have lazy loading disabled and do not have any properties marked a virtual.
Is there anyway to keep it from doing this? I did some searching and most people are asking how to get child objects to populate, not how to keep them from populating when they're not requested.
UPDATE: To further explain, this is happening before any sort of serialization by ASP.NET, immediately after it comes from the data context, and we are not getting any reference loops when the objects are serialized from WebAPI.