获取外键对象的主要解决方案是什么?
我有两个表:
人员:(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要一个包含所有没有地址的人员的人员集合。
您还可以创建一个虚拟地址并将其与所有没有该地址的人员相关联。
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.
您应该做的是与当前映射相反的操作,即,
这样您就可以将该人映射到他的地址。如果一个人有多个地址,您可以使用地址集合。
另外,类名应保持单数形式,例如 Person 而不是 Persons,因为每个对象都引用单个实体。
What you should be doing is the reverse of the current mapping, ie.,
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.
我搜索并找到了这个解决方案:
I searched and found this solution: