0

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.

Daniel Schilling
  • 4,829
  • 28
  • 60
Daniel T.
  • 37,212
  • 36
  • 139
  • 206

2 Answers2

0

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
  • 14,173
  • 2
  • 42
  • 60
  • 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. Jan 03 '10 at 05:05
  • You're already coupled to Iesi.Collections by using NHibernate. – James Gregory Jan 04 '10 at 09:40
0

In response to Christina's question, you have to create a new class that implements IAutoMappingOverride<T>:

public class AlbumOverride : IAutoMappingOverride<Album>
{
    public void Override(AutoMapping<Album> mapping)
    {
        mapping.HasMany(x => x.Pictures).AsSet().Inverse();
    }
}

Then tell FNH to use it in the configuration:

Fluently.Configure()
    .Database(...)
    .Mappings(m => m.AutoMappings.Add(AutoMap.Assemblies(...)
        .UseOverridesFromAssemblyOf<Album>()))
    .BuildConfiguration();

You'll need a new override class for every entity you need an override for, but it's mostly a copy and paste affair.

Community
  • 1
  • 1
Daniel T.
  • 37,212
  • 36
  • 139
  • 206