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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With