0
votes

I have 3 tables: Courses, Departments, and Universities.

When I pull the information from the database (using entity framework), I am attempting to map, for example, a Course to a CourseDTO. I am trying to set it up in such a way that the CourseDTO automatically checks if the relationships exist (is not null), then hook up the relationship in the DTO.

Problem is once I run:

db.Courses.Include(c=>c.Department).Include(c=>c.University)

It maps every nested permutation of these relationships, so my automatic mapping to the DTO just creates a stackoverflow because it goes on forever. (eg. Course maps the Department which maps the Courses in that Department, which maps the Department, etc.)

What is the best solution to avoid this problem?

1

1 Answers

0
votes

I would suggest turning off Lazy Loading for EntityFramework via

context.Configuration.LazyLoadingEnabled = false

This will prevent the sub-objects that you did not explicitly include from being expanded by Entity Framework when they are accessed.

Either that or create a custom mapping method to include only the objects you want in your DTO.