Can I define eager loading per datacontext instead per query?
Is there a way to define eager loading per entire data conte开发者_JAVA技巧xt, 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.
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 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
You can define the data load options in the constructor of your DataContext:
public MyDataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString, mappingSource)
{
DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Customer>(o => o.City);
this.LoadOptions = options;
OnCreated();
}
This will eager load the attribute every time.
精彩评论