views:

214

answers:

1

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.

A: 

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.

James Gregory
Thanks, I had a feeling that was the case. Unfortunately, `System.Collections.Generic.HashSet` directly implements `ICollection` and there's no `ISet`, so it's either go back to lists or use `Iesi.Collections.ISet` with `HashedSet`, but then that couples you to Iesi.
Daniel T.
You're already coupled to Iesi.Collections by using NHibernate.
James Gregory