3

How can I use .Include() in order to include all lists of relationships of a table (class) in Entity Framework?

I saw this option:

var coursesWithStudents = context.courses.Include("students").ToList();

But I need something generic, not just for a specific table, and to include all of the relationships of the table.

Is there any way to do that?

Thanks all.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TOMER
  • 73
  • 4
  • How will you then make use of it? – Caius Jard May 26 '22 at 03:49
  • I need to use it in a complex genetic algorithm. I have many tables and relationships so I have to find some clean way to do that. – TOMER May 26 '22 at 04:02
  • Hmm, I think the design goal of EF is generally that developer makes these choices manually but if you're looking to write code that makes them, it would perhaps be possible to reflect on the EF entity looking for navigation properties and programmatically build a huge list of includes.. That said, it could potentially cause all of the database to be downloaded into the client.. Be careful what you wish for?! :) – Caius Jard May 26 '22 at 04:39
  • Yeah, I understand what you say... You're right. But even though - if you have any idea - I will be thankful. Because I do not want to group all the tables into noe huge list... I just want to get access from a table to its lists of relationships. Thank you Caius Jard! – TOMER May 26 '22 at 05:31

1 Answers1

2

If you want to get related entities in generic form. I think you could see high score solution of question and it will help you. EF Including Other Entities (Generic Repository pattern)

Yusif Karimov
  • 400
  • 5
  • 8