To add to Kamyar's answer...
The AllIncluding method is only available if you are using MVC scaffolding. see the following link for a listing of the method:
Mvc 3 Scaffolding: the Model passed to the View throws SQL errror
I tried using it, but still encountered the circular reference error, since the root objects were still being returned as proxies. So I customised the method to temporarily turn off the ProxyCreationEnabled flag on the EF context, and eagerly load the specified properties listed in the method's parameter. See the following link for further details:
Loading from database without proxy classes?
In order for this to work, the query had to be performed while the setting was still off, so I had to call the query's ToList() method to perform the query, and then returned the IEnumerable, rather than IQueryable. This did the job for me.
Here is the method I used ("_context" is the variable name for my EF context):
public IEnumerable<TEntity> ListIncluding<TEntity>(params Expression<Func<TEntity, object>>[] includeProperties)
where TEntity : class
{
bool cachedSetting = _context.Configuration.ProxyCreationEnabled;
_context.Configuration.ProxyCreationEnabled = false;
IQueryable<TEntity> query = _context.Set<TEntity>();
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
IEnumerable<TEntity> list = query.ToList();
_context.Configuration.ProxyCreationEnabled = cachedSetting;
return list;
}
This can then get called using the following syntax:
IEnumerable<News> newsItems = newsRepository.ListIncluding<News>(news => news.Category, news => news.Image);