I've run into some memory problems while using EF4.1, the problems mainly happens in this situation: Imagine that I have Students, that can attend one or more Courses, and multiple users can attend the same course. So, I have something like:
Student * < - > 1-* Course
Imagine that I have in my BD 2 students and 1 course. Like this:
Ana Attends English Course Bob Attends English Course
My Object Graph is something like this:
Ana
\
English Course
/
Bob
This is fine.
I save this and that's saving fine, two lines on the student table, and one on the courses table.
The problem is when I try to get this data.
When I do something like:
var students = (from s in students
select s).Include("Courses");
This is the resulting graph:
Ana -> English Course
Bob -> English Course
The object is duplicated. Imagine the situation when the depth of this tree is much bigger, and there are thousands of students and thousands of courses, and hundreds of student attending the same course.
The memory usage of this query would be huge, how to solve this reference problem?