Convention for collection with backing field prefixed with underscore
How do I map the collection of Parts using a convention
public class Part
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class Car
{
private readonly List<Part> _parts = new List<Part>();
public virtual int Id { get; set; }
public virtual IList<Part> Parts
{
get { return _parts.AsReadOnly(); }
}
}
I have tried this convention but it always expects the field name to not have an underscore prefix:
public class HasManyConvention : IHasManyConvention
{
#region IHasManyConvention Members
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Access.ReadOnlyPropertyThroughCamelCaseField(CamelCasePrefix.Underscore);
}
#endregion
}
I've tried it with the 1.10.685 release and also the latest build 1.1.0.695, with the same result:
"Could not find field 'parts' in class 'TestFluentNHibernate.Car'"
See the attached program which reproduces this problem.
- Program.cs 2.1 KB
2 Posted by Mark Bryant on 10 Nov, 2010 12:39 PM
Remove CamelCasePrefix.Underscore. There is an overload that takes no arguments.
3 Posted by Mark Bryant on 10 Nov, 2010 01:21 PM
apologies, I had another look at your post and realised I have missed the point.
4 Posted by Chris Richards on 26 Jan, 2011 10:30 AM
Problem still exists with 1.2.0.694 and also 2.0.0.698. The attached Visual Studio 2010 project uses the same code as above to demonstrate this problem.
5 Posted by mark.bryant on 02 Feb, 2011 11:40 AM
I have had some success with inheriting from DefaultAutomappingConfiguration
then:
public override FluentNHibernate.Mapping.Access GetAccessStrategyForReadOnlyProperty(FluentNHibernate.Member member)
You can then apply a different strategy based on the collection type.