获取外键对象的主要解决方案是什么?

发布于 2024-12-31 23:48:58 字数 418 浏览 1 评论 0原文

我有两个表:

人员:(Id,名称)

地址:(Id,地址,PersonID)

这意味着每个人都有一些地址。 我有与每个表相关的对象和每个外键对象的属性。在此示例中:

Class Persons
{
    int     id;
    string  name;
}

Class Addresses
{
    int     id;
    string  address;
    Persons person;
}

当我想要获取所有人员的地址时,我使用左连接。我创建了一个 Address 类的集合,并使用“person”属性访问每个 Person。

但在某些情况下,有些人没有任何地址。我如何使用此方法访问我的对象中的那些人。对于这种情况最好的方法是什么?

谢谢,

I have two tables:

Persons: (Id, Name)

Addresses: (Id, Address, PersonID)

It means each person has some addresses.
I have object related to each table and a property for each foreign key object. In this example:

Class Persons
{
    int     id;
    string  name;
}

Class Addresses
{
    int     id;
    string  address;
    Persons person;
}

When I want to get all of my persons with their addresses I use left join. I create a collection of Address class and reach to each Person using "person" property.

But in some cases there are some persons that don't have any addresses. How can I access to that persons in my objects using this method. What is the best method for this situation?

Thanks,

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

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

发布评论

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

评论(3

陈甜 2025-01-07 23:48:58

您需要一个包含所有没有地址的人员的人员集合。
您还可以创建一个虚拟地址并将其与所有没有该地址的人员相关联。

You need a collection of Persons that contains all the Persons that don't have address.
You also can create a dummy address and associate this to all the Persons that don't have it.

忆梦 2025-01-07 23:48:58

您应该做的是与当前映射相反的操作,即,

Class Person
{
    int     Id;
    string  name;
    Address address;
}

Class Address
{
    int     Id;
    string  Address;
}

这样您就可以将该人映射到他的地址。如果一个人有多个地址,您可以使用地址集合。

另外,类名应保持单数形式,例如 Person 而不是 Persons,因为每个对象都引用单个实体。

What you should be doing is the reverse of the current mapping, ie.,

Class Person
{
    int     Id;
    string  name;
    Address address;
}

Class Address
{
    int     Id;
    string  Address;
}

This way you can map the person to his address. In case a single person has multiple addresses you can use a collection of Address.

Also keep singular for your class names like Person instead of Persons since each object refers to a single entity.

无言温柔 2025-01-07 23:48:58

我搜索并找到了这个解决方案:

Class Persons
{
    int     id;
    string  name;
    AddressesCollection addresses;
}

Class Addresses
{
    int     id;
    string  address;
    Persons person;
}

I searched and found this solution:

Class Persons
{
    int     id;
    string  name;
    AddressesCollection addresses;
}

Class Addresses
{
    int     id;
    string  address;
    Persons person;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文