I seem to be having trouble modeling inherited classes using JoinedSubClassPart in Fluent NHibernate (release 524). I have seen other posts indicating that the approach I am using (see below) should work, yet at runtime I get the following exception (thrown by ImappingPart.PositionOnDocument method of JoinedSubClassPart):
XunitException: FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
---- System.InvalidOperationException : Failed to compare two elements in the array. -------- System.NotSupportedException : Obsolete
Should this in fact work or has this been deprecated in favour of another approach?
I am hoping I am simply doing something wrong as I do like the idea of being able to add subclass mapping dynamically via AddPart().
Any help will be greatly appreciated!
A simplified version of my object model (which still fails) is as follows:
public class MyBaseClass
{
public int Id { get; set; }
public string CommonProperty { get; set; }
}
public class MySubClass1 : MyBaseClass
{
public string MySubClass1Property { get; set; }
}
public class MySubClass2 : MyBaseClass
{
public string MySubClass2Property { get; set; }
}
And my mappings are as follows:
public class MyBaseClassMap : ClassMap<MyBaseClass>
{
public MyBaseClassMap()
{
Not.LazyLoad();
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.CommonProperty);
AddPart(new MySubClass1Map());
AddPart(new MySubClass2Map());
}
}
public class MySubClass1Map : JoinedSubClassPart<MySubClass1>
{
public MySubClass1Map()
: base("MyBaseClassId")
{
Not.LazyLoad();
Map(x => x.MySubClass1Property);
}
}
public class MySubClass2Map : JoinedSubClassPart<MySubClass2>
{
public MySubClass2Map()
: base("MyBaseClassId")
{
Not.LazyLoad();
Map(x => x.MySubClass2Property);
}
}
After digging a little deeper, I found this fluent-nhibernate issue which explains that, as of r526 AddPart has been deprecated in favour of JoinedSubClass. After modifying my code to that shown below, I was able to get the modeling I was looking for.
In my base class map, I replaced the calls to AddPart with:
JoinedSubClass("MyBaseClassId", MySubClass1Map.AsJoinedSubClass());
JoinedSubClass("MyBaseClassId", MySubClass2Map.AsJoinedSubClass());
And my subclass maps changed to:
public class MySubClass1Map
{
public static Action<JoinedSubClassPart<MySubClass1>> AsJoinedSubClass()
{
return sub =>
{
sub.Map(x => x.MySubClass1Property);
};
}
}
public class MySubClass2Map
{
public static Action<JoinedSubClassPart<MySubClass2>> AsJoinedSubClass()
{
return sub =>
{
sub.Map(x => x.MySubClass2Property);
};
}
}