Search code examples
fluent-nhibernatehashsetautomapping

Is it possible to use a Fluent NHibernate convention to map all ICollections as sets?


Is it possible to use a Fluent NHibernate convention to map all ICollections as sets? I have an entity like so:

public class NoahsArk
{
    public virtual ICollection<Animal> Animals { get; set; }

    public NoahsArk()
    {
        Animals = new HashSet<Animal>();
    }
}

With fluent mappings, this property would be mapped as HasMany(x => x.Animals).AsSet(), but how would I do this with a convention that I want to use with the automapper?

I should add that by default, ICollections get persisted as ILists, and I get a cast exception when it tries to cast the HashSet to IList.


Solution

  • This isn't possible in a convention, currently. If you want the automapper to treat your collections as sets by default, use ISet instead of ICollection.