Orchard eager loading when using IRepository

While developing a custom module for Orchard CMS version 1.7 I created a simple 1 to many relationship as described in this article Creating 1 n and n n relations.

So I had a parent record with a property pointing to the child record:

public class ChildRecord
{
	public virtual int Id { get; set; }
	public virtual string Name { get; set; }
}
public class ParentRecord
{
	public virtual int Id { get; set; }
	public virtual ChildRecord Child{ get; set; }
	public virtual bool SelectCondition { get; set; }
}

Then, inside a controller I used the repository to get all the parent entities that satisfy a condition. Something like this:

var items = parentRepository.Table.
              Where(item => item.SelectCondition == true).ToList();

This worked fine but I wanted to eagerly load the child entity together with the parent.

The first thing I tried was to add the Aggregate attribute to the Child property inside the ChildRecord class:

public class ParentRecord
{
	public virtual int Id { get; set; }
	[Aggregate]
	public virtual ChildRecord Child{ get; set; }
	public virtual bool SelectCondition { get; set; }
}

Although the description of this attribute says:

This attribute is used to mark relationships which need to be eagerly fetched with the parent object, thus defining an aggregate in terms of DDD.

this didn’t work for me.

The solution is quite simple, but I’m posting it here just because I had some trouble finding it:

Use the Fetch extension method defined inside the class EagerFetchingExtensionMethods under the NHibernate.Linq namespace.

var items = parentRepository.Table.
              Where(item => item.SelectCondition == true).
              Fetch(c => c.ChildRecord).ToList();

Orchard – 404 errors

Orchard - 404 error

While developing a new custom module for Orchard and running the site locally I noticed that it wasn’t working any more:

  • all the pages returned 404 error messages
  • the home page displayed the directory listing

I assumed it was something wrong with my module so I started to search for the cause. I found some suggestions like:

  • make sure you have a content page that has the homepage property set to true
  • delete the cache file: Orchard.Web\App_Data\cache.dat
  • check for duplicated route names
  • check the log file under Orchard.Web\App_Data\Logs

And finally, the log file saved me, showing that there was a DB connection problem.

Note to myself: Always check the log file first if you have no idea what’s going wrong.