References/has-a mapping thru 3 tables

穿精又带淫゛_ 提交于 2019-12-11 06:16:31

问题


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?

This question is related.


回答1:


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");
        });
    }
}


来源:https://stackoverflow.com/questions/5931917/references-has-a-mapping-thru-3-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!