Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define eager loading per datacontext instead per query?

Is there a way to define eager loading per entire data context, so I could state something like this -- "when using this data context when loading Customer always load associated City as well"?

So, something very similar to Options and LoadWith in Linq to Sql.

like image 325
greenoldman Avatar asked Jun 23 '26 03:06

greenoldman


2 Answers

No Entity framework doesn't offer equivalent to DataLoadOptions available in Linq-to-Sql. There is no way to define something like LoadWith or AssociateWith globally.

like image 51
Ladislav Mrnka Avatar answered Jun 24 '26 17:06

Ladislav Mrnka


Like Ladislav said, there currently is no way to do this in EF. However, the question comes back to why you want to do this globally rather than on a per-query basis.

If your main purpose is to abstract away eager loading out of your business layer, one options is to do something like I describe on in a blog post I wrote here. Essentially it describes a library that I want to create (time permitting) where it automatically determines how to eager load by looking at the data structure a query should be based around.

However, I don't know if that would solve the core issue of what you are trying to do or not without more detail of why you want this globally

like image 24
KallDrexx Avatar answered Jun 24 '26 17:06

KallDrexx