Mapping collection of strings with NHibernate

I just ran into a similar situation; and I found that it is indeed possible to map a collection of strings.
Note that you’ll have to map those strings as value objects.

This is what I have:

public class Chapter
{
    private ISet<string> _synonyms = new HashedSet<string>();

    public ReadOnlyCollection<string> Synonyms
    {
       get { return new List<string>(_synonyms).AsReadOnly(); }
    }
}

Mapping:

<class name="Chapter" table="Chapter">
   <set name="Synonyms" table="ChapterSynonyms">
       <key column="ChapterId" />
       <element column="ChapterCode" type="string" />
   </set>
</class>

Leave a Comment