问题
I am having some problems mapping the following scenario in Fluent Nhibernate using Table Per Concrete class:
Let's say I have the following class definitions:
public class Reading { .... }
public class CarReading : Reading { .... }
public class TruckReading : Reading { .... }
public class Alert
{
....
public virtual Reading AReading { get; set; }
}
So my question is how to create the mapping class for Alert, if it has a one to one relationship with reading class (could be either truck reading or car reading) and instruct nhibernate to know which table to load the data from (TruckReading table or CarReading Table)
public class AlertMap : ClassMap<Alert>
{
....
HasOne(x => x.AReading);
}
If anyone could point me in the right direction that would be much appreciated.
Thanks.
回答1:
public class AlertMap : ClassMap<Alert>
{
....
ReferenceAny(x => x.AReading)
.EntityIdentifierColumn("readingid")
.EntityTypeColumn("readingtype")
.IdentityType<int>()
.AddMetaValue<CarReading>("car")
.AddMetaValue<TruckReading>("truck");
}
来源:https://stackoverflow.com/questions/12812042/fluent-nhibernate-polymorphic-mapping-challenges