5

I have the following in my NHibernate entity:

    private ICollection<string> _stringSet = new HashSet<string>();
    public virtual ICollection<string> StringSet
    {
        get { return _stringSet; }
    }

Then, in my Fluent mapping I map it like so:

    HasMany(x => x.StringSet)
        .Table("String_Set")
        .Element("StringValue")
        .AsSet();

Unfortunately, when I try to save the entity, I get an ArgumentNullException saying the "Collection cannot be null." I do not get this error if I default my field to a List, but as you see in my mapping, I want Set behavior, even from an unsaved entity. It appears to me to be a problem with HashSet<> not implementing the non-generic ICollection. What is the proper way to have Set behavior for unsaved entities? I would also prefer to retain the ICollection<string> as my exposed type.

StarKat99
  • 531
  • 3
  • 15
  • 1
    can you post the whole stacktrace? – Vadim Jul 13 '11 at 18:16
  • Aha. I thought I had stripped away all the Fluent conventions, but apparently there was a collection convention setting it to .AsBag(). Works as expected once the convention was removed, although it seems like a bug (or at least unexpected behavior) that the .AsSet() was not overriding the .AsBag() convention. – StarKat99 Jul 13 '11 at 18:56
  • 1
    feel free to answer your own question and mark it as accepted. – Vadim Jul 13 '11 at 21:42
  • Apparently I don't have enough rep to answer my own question, sadly. – StarKat99 Jul 13 '11 at 22:29
  • You just need to wait 8 hours before you can answer your own question with rep less than 100. – Vadim Jul 14 '11 at 14:44
  • And now I can't mark the answer in for another 22 :( – StarKat99 Jul 14 '11 at 18:48

1 Answers1

5

Aha. I thought I had stripped away all the Fluent conventions, but apparently there was a collection convention setting it to .AsBag(). Works as expected once the convention was removed, although it seems like a bug (or at least unexpected behavior) that the .AsSet() was not overriding the .AsBag() convention.

StarKat99
  • 531
  • 3
  • 15