Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collection cannot be null exception when using HashSet in NHibernate

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.

like image 584
StarKat99 Avatar asked Jul 13 '11 17:07

StarKat99


1 Answers

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.

like image 85
StarKat99 Avatar answered Sep 30 '22 08:09

StarKat99