My model object Reading
has a Location
but it's not a direct relationship in the database. In the DB, this "has-a" relationship or "reference" spans 3 tables, as shown in this snip:
My Reading
maps to the ComponentRea开发者_开发知识库ding table and i want my Location
to map to the Location table. My ClassMap<Reading>
class looks like this for now:
public class ReadingMap : ClassMap<Reading>
{
public ReadingMap()
{
Table("ComponentReading");
Id(x => x.ID).Column("ComponentReadingId");
//References(x => x.Location).Formula(
Join("VehicleReading", vr =>
{
Join("TrainReading", tr =>
{
tr.References(x => x.Location, "LocationId");
});
});
Map(x => x.TemperatureValue).Column("Temperature");
}
}
And here is my simple Location
mapping:
public class LocationMap : ClassMap<Location>
{
public LocationMap()
{
Id(x => x.ID).Column("LocationId");
Map(x => x.Name);
}
}
The commented References(
method sort of shows what i want to achieve with the relationship between Reading
and Location
but obviously i can't express it to FNH as simply as the commented line suggests.
I don't think the Join(
code is even nearly correct either, but it also tries to communicate the relationship that i'm after.
I hope someone can see what i'm trying to do here. Can you help me?
This question is related.
I think you cant nest joins that way. An ugly but pragmatic solution would be (untested):
class Reading
{
public virtual int ID { get; set; }
protected virtual Hidden.TrainReading m_trainReading;
public virtual Location Location
{ get { return m_trainReading.Location; } set { m_trainReading.Location = value; } }
public virtual int TemperatureValue { get; set; }
}
namespace Hidden
{
class TrainReading
{
public virtual int ID { get; set; }
public virtual int VehicleReadingId { get; set; }
public virtual Location Location { get; set; }
}
}
public class ReadingMap : ClassMap<Reading>
{
public ReadingMap()
{
Table("ComponentReading");
Id(x => x.ID).Column("ComponentReadingId");
References(Reveal.Member<Reading, Hidden.TrainReading>("m_trainReading"), "");
Map(x => x.TemperatureValue).Column("Temperature");
}
}
public class TrainReadingMap : ClassMap<Hidden.TrainReading>
{
public TrainReadingMap()
{
Table("TrainReading");
Id(x => x.ID).Column("TrainReadingId");
References(x => x.Location, "LocationId");
Join("VehicleReading", vr =>
{
vr.KeyColumn("TrainReadingId");
vr.Map(x => x.VehicleReadingId, "VehicleReadingId");
});
}
}
精彩评论