Mapping a list of components with Fluent NHibernate

This took me some time to figure out since documentation for Fluent NHibernate is still a little scarce. Let’s say we have an entity “ActivityType” which has an icon and a name, but the name is stored for each language so we have a one to many relationship. This relationship is not only modelled in the entity “ActivityType” but also in several others. The translation is modelled in the “LocalizedEntity” class. As illustrated in the class diagram below:

The DB is illustrated in the next diagram, to make it easy the tables have a different name compared to the entity diagram :p.

The mapping for the “Language” entity is pretty straightforward. The mapping for the “ActivityType” and its localizations was a bigger challenge since I got lost in all the delegates after the Component method.

public class LanuageMapping
: ClassMap<Language>
{
	public LanuageMapping()
	{
		Id(x => x.Id);
		Map(x => x.Name);
		WithTable("[7de_Languages]");
	}
}
 
public class ActivityTypeMapping
: ClassMap<ActivityType>
{
	public ActivityTypeMapping()
	{
		WithTable("[7de_EventTypes]");
		Id(activityType => activityType.Id);
		Map(activityType => activityType.IconName);
		HasMany<LocalizedEntity>(activityType => activityType.Descriptions)
		.WithTableName("[7de_LocalizedEventTypes]")
		.Component(description => 
				   {
				   description.References<Language>(desc => desc.Language)
				   .CanNotBeNull();
				   description.Map(desc => desc.Localization)
				   .CanNotBeNull();
				   });
	}
}

3 Replies to “Mapping a list of components with Fluent NHibernate”

  1. Hi, i was mapping a similar model, but the problem is that, using an analogy with your example it says that LocalizedEntity is not mapped.

Leave a Reply

Your email address will not be published. Required fields are marked *