Most likely the problem you are experiencing is a Cartesian product.
Based on just some sample data:
var query = ctx.Questions // 50
.Include("Attachments") // 20
.Include("Location") // 10
.Include("CreatedBy") // 5
.Include("Tags") // 5
.Include("Upvotes") // 5
.Include("Upvotes.CreatedBy") // 5
.Include("Downvotes") // 5
.Include("Downvotes.CreatedBy") // 5
// Where Blah
// Order By Blah
This returns a number of rows upwards of
50 x 20 x 10 x 5 x 5 x 5 x 5 x 5 x 5 = 156,250,000
Seriously... that is an INSANE number of rows to return.
You really have two options if you are having this issue:
First: The easy way, rely on Entity-Framework to wire up models automagically as they enter the context. And afterwards, use the entities AsNoTracking()
and dispose of the context.
// Continuing with the query above:
var questions = query.Select(q => q);
var attachments = query.Select(q => q.Attachments);
var locations = query.Select(q => q.Locations);
This will make a request per table, but instead of 156 MILLION rows, you only download 110 rows. But the cool part is they are all wired up in EF Context Cache memory, so now the questions
variable is completely populated.
Second: Create a stored procedure that returns multiple tables and have EF materialize the classes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…