引用/ hasa映射通过3个表。

时间:2021-10-30 11:31:38

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:

我的模型对象读取有一个位置,但它不是数据库中的直接关系。在DB中,这个“has-a”关系或“reference”跨越了3个表,如下面的剪切所示:

引用/ hasa映射通过3个表。

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:

我的读取映射到组件读取表,我希望我的位置映射到位置表。我的ClassMap <读取> 类现在看起来是这样的:

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.

注释引用(方法显示了我想通过读取和位置之间的关系来实现的东西,但是很明显,我不能像注释行建议的那样简单地将它表达给FNH。

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 个解决方案

#1


0  

I think you cant nest joins that way. An ugly but pragmatic solution would be (untested):

我认为你不能用那种方式加入nest。一个丑陋但务实的解决方案将是(未经检验的):

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

#1


0  

I think you cant nest joins that way. An ugly but pragmatic solution would be (untested):

我认为你不能用那种方式加入nest。一个丑陋但务实的解决方案将是(未经检验的):

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