Suppose I have three classes. It is valid to instantiate A, but there are also special cases B and D which subclass A, adding additional information.
How would I do the mapping files for this in (fluent) NHibernate?
public class A { public int ID { get; set;} public string CommonProperty1 { get; set; } public string CommonProperty2 { get; set; } } public class B : A { public string BSpecificProperty1 { get; set; } //not null public string BSpecificProperty2 { get; set; } //not null } public class D : A { public string DSpecificProperty { get; set; } //not null }
I tried the following, but it doesn’t work at all:
public class AMap : ClassMap<A> { public AMap() { Id(x => x.ID); Map(x => x.CommonProperty1); Map(x => x.CommonProperty2); } } public class BMap : ClassMap<B> { public BMap() { References(x => x.ID); Map(x => x.BSpecificProperty1) .CanNotBeNull(); Map(x => x.BSpecificProperty2) .CanNotBeNull(); } } public class DMap : ClassMap<D> { public DMap() { References(x => x.ID); Map(x => x.DSpecificProperty) .CanNotBeNull(); } }
I’m not sure I understand what you mean by ‘map a subclass one-to-one’, but if you want to map inheritance where the subclasses have properties that are not nullable, you can do like this in Fluent-NHibernate:
Since you want the subclasses’ properties to be not-null, you have to use the table-per-class (joined-subclass) way of modeling the inheritance. This is because table-per-hierarchy requires all subclass properties to be nullable.
I hope it helps.
/Erik