问题描述
我的模型对象阅读有一个位置,但它不是数据库中的直接关系。在DB中,这个有-a关系或引用跨越了3个表格,如下所示:
阅读
>映射到ComponentReading表,我希望我的 Location 映射到Location表。
public class ReadingMap:ClassMap< Reading>
{
public ReadingMap()
{
Table(ComponentReading);
Id(x => x.ID).Column(ComponentReadingId);
参考(x => x.Location).Formula(
Join(VehicleReading,vr =>
{
Join(TrainReading ,tr =>
{
tr.References(x => x.Location,LocationId);
});
});
Map(x => x.TemperatureValue).Column(Temperature);
}
}
以下是简单的位置映射:
public class LocationMap:ClassMap< Location>
{
public LocationMap()
{
Id(x => x.ID).Column( LocationId);
Map(x => x.Name);
}
}
引用(方法排序显示了我想用读取和 Location ,但是显然我不能像FNH那样向FNH表示我不认为加入()代码甚至几乎是正确的,但它也是试图传达我之后的关系。
我希望有人能看到我在这里想要做什么。你能帮我吗?
我认为你不能这样嵌套连接。一个丑陋但实用的解决方案将是(未经测试):
$ $ p $ $ $ $ {get;组; }
保护虚拟Hidden.TrainReading m_trainReading;
public virtual Location Location
{get {return m_trainReading.Location; } set {m_trainReading.Location = value; }}
public virtual int TemperatureValue {get;组; }
$ b命名空间隐藏
{
class TrainReading
{
public virtual int ID {get;组; }
public virtual int VehicleReadingId {get;组; }
public virtual Location Location {get;组; }
}
}
public class ReadingMap:ClassMap< Reading>
{
public ReadingMap()
{
Table(ComponentReading);
Id(x => x.ID).Column(ComponentReadingId);
参考文献(Reveal.Member< Reading,Hidden.TrainReading>(m_trainReading),);
Map(x => x.TemperatureValue).Column(Temperature);
}
}
public class TrainReadingMap:ClassMap< Hidden.TrainReading>
{
公共TrainReadingMap()
{
表(TrainReading);
Id(x => x.ID).Column(TrainReadingId);
参考文献(x => x.Location,LocationId);
Join(VehicleReading,vr =>
{
vr.KeyColumn(TrainReadingId);
vr.Map(x => x。 VehicleReadingId,VehicleReadingId);
});
}
}
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 ComponentReading 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?
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"); }); } }
这篇关于引用/有一个映射通过3个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!