References/has-a 通过 3 个表进行映射

发布于 2024-11-05 19:58:46 字数 1425 浏览 3 评论 0原文

我的模型对象 Reading 有一个 Location 但它在数据库中不是直接关系。在数据库中,此“has-a”关系或“引用”跨越 3 个表,如以下片段所示:

输入图像描述这里

我的 Reading 映射到 ComponentReading 表,我希望我的 Location 映射到 Location 表。我的 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");
    }
}

这是我的简单 Location 映射:

public class LocationMap : ClassMap<Location>
{
    public LocationMap()
    {
        Id(x => x.ID).Column("LocationId");
        Map(x => x.Name);
    }
}

带注释的 References( 方法显示了什么 想实现 ReadingLocation 之间的关系,但显然我无法像注释行所暗示的那样简单地向 FNH 表达它。

Join( 代码甚至几乎是正确的,但它也试图传达我所追求的关系。

我希望有人能看到我在这里想做的事情。你能帮助我吗?

< a href="https://stackoverflow.com/q/5906163/56145">这个问题是相关的。

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:

enter image description here

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在风中等你 2024-11-12 19:58:46

我认为你不能以这种方式嵌套连接。一个丑陋但实用的解决方案是(未经测试):

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

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");
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文