Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping collection of strings with NHibernate

I have a domain class with a property IList<string> that I want to map to a table with a single data value (i.e. it has an ID, a foreign key ID to the domain entity table, and a varchar data column).

I keep getting the error:

Association references unmapped class: System.String

How can I map a table to a collection of strings?

like image 724
roryf Avatar asked Mar 03 '09 14:03

roryf


People also ask

What is NHibernate mapping?

nhibernate Mappings Xml Mappings It is a syntax xml file which contains the metadata required for the object/relational mapping. The metadata includes declaration of persistent classes and the mapping of properties (to columns and foreign key relationships to other entities) to database tables.


1 Answers

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>
like image 97
Frederik Gheysels Avatar answered Oct 19 '22 15:10

Frederik Gheysels