I'm trying to map (by code) a protected collection to a bag but I'm struggling. e.g.
public class MyClass
{
....
protected virtual ICollection<Items> MyItems { get; set; }
....
}
public class MyClassMapping : ClassMapping<MyClass>
{
...
Bag(x => x.MyItems, map =>
{
....
}
...
}
It throws a mapping exception with the inner exception being "ArgumentNullException: Value cannot be null. Parameter name: localMember". It works fine if the "MyItems" collection is public.
I followed this article (https://groups.google.com/forum/#!topic/nhusers/wiH1DPGOhgU) which recommends using the method overload that takes a string. e.g.
public class MyClassMapping : ClassMapping<MyClass>
{
...
Bag("MyItems", map =>
{
....
}
...
}
But this gives a compilation error "The type arguments for method .... cannot be inferred from the usage. Try specifying the type arguments explicitly".
Is it possible to map to a protected collection (I'm using NH 3.3)? Can someone give me an example?
Thanks, Chet
As we can see the overloaded method here: PropertyContainerCustomizer.cs
public void Bag<TElement>(string notVisiblePropertyOrFieldName
, Action<IBagPropertiesMapper<TEntity, TElement>> collectionMapping
, Action<ICollectionElementRelation<TElement>> mapping)
{ ... }
What we have to pass as the generic template is the TElement
, the one used as ICollection<TElement>
.
And because the defintion is:
// TElement is Items
protected virtual ICollection<Items> MyItems { get; set; }
SOLUTION: What we have to do is declare the mapping like this
// the TElement must be expressed explicitly as Items
Bag<Items>("MyItems", map =>
{
....
}