双向映射

发布于 2024-11-27 02:10:05 字数 468 浏览 0 评论 0原文

有什么办法,如何创建两个类,这两个类将被两种方式引用并且仅使用一个 FK?这让我对一对一和一对多的情况感兴趣。

铁:

Class First: Entity
{
Second second;
}

Class Second: Entity
{
First first;
}

String TwoWayReference()
{
First Fir = new First();
Second Sec = new Second();

Fir.second = Sec; // I need it is equivalent to: Sec.first = Fir;

if (Sec.first == Fir)
    return "Is any way how to do this code works and code return this string?";
else
    return "Or it is impossible?"
}

Is any way, how to create two classes, which will be referenced both way and will use only one FK? This interest me in One-to-One as like as One-to-Many cases.

f.e.:

Class First: Entity
{
Second second;
}

Class Second: Entity
{
First first;
}

String TwoWayReference()
{
First Fir = new First();
Second Sec = new Second();

Fir.second = Sec; // I need it is equivalent to: Sec.first = Fir;

if (Sec.first == Fir)
    return "Is any way how to do this code works and code return this string?";
else
    return "Or it is impossible?"
}

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

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

发布评论

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

评论(1

岁月静好 2024-12-04 02:10:05

最简单的是

class First : Entity
{
    private Second second;
    public virtual Second Second
    {
        get { return this.second; }
        set {
            if (value != null)
            {
                value.First = this;
                this.second = value;
            }
        }
    }
}

class Second : Entity
{
    private First first;
    public virtual First First
    {
        get { return this.first; }
        set {
            if (value != null && value.Second != this)
            {
                value.Second = this;
                this.first = value;
            }
        }
    }
}

simplest would be

class First : Entity
{
    private Second second;
    public virtual Second Second
    {
        get { return this.second; }
        set {
            if (value != null)
            {
                value.First = this;
                this.second = value;
            }
        }
    }
}

class Second : Entity
{
    private First first;
    public virtual First First
    {
        get { return this.first; }
        set {
            if (value != null && value.Second != this)
            {
                value.Second = this;
                this.first = value;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文