简体   繁体   English

与许多表相关的一对多关系

[英]One-to-many relationship related to many tables

I have a scenario where: there are two (or more) tables that represent independent items. 我有一个场景:有两个(或更多)表代表独立的项目。 lets say Users and Companies 可以说用户和公司

Both of these tables need addresses stored. 这两个表都需要存储地址。 Each one can have one or more address 每个人可以有一个或多个地址

In a normal 1 to many scenario Addresses table woudl just have a UserId or a CompanyId creating a normal 1 to many relationship. 在正常的一对多情况下,Addresss表woudl仅具有一个UserId或CompanyId,可创建一个正常的一对多关系。

In this case i have a few approaches i can think of 在这种情况下,我可以想到几种方法

  1. the Addresses table could have both a UserId and a CompanyId and only one would be used for each record. 地址表可以同时具有UserId和CompanyId,并且每条记录只能使用一个。

  2. 2 keys could be used ObjectId and ObjectType So Object id would have a UserId or a CompanyId, and ObjectType woudl be User or Company 可以使用2个键来使用ObjectId和ObjectType,因此Object id可以具有UserId或CompanyId,而ObjectType可以是User或Company

  3. Create an ObjectTable and add ObjectId to Users and Companies. 创建一个ObjectTable并将ObjectId添加到用户和公司。 Addresses would then have an OjbectId 地址将具有一个OjbectId

I do not really like any of these solutions. 我真的不喜欢这些解决方案。 i am wondering what is the best approach here. 我想知道什么是最好的方法。

On another note i will most likely user linqtosql for my data access layer. 另一方面,我很可能将用户linqtosql用于我的数据访问层。

I'm not sure about the implications regarding Linq-to-SQL, but one model for solving this problem is to use multiple Junction Tables . 我不确定有关Linq-to-SQL的含义,但是解决此问题的一种模型是使用多个Junction Tables

In your case, you would have a table called AddressUsers that has two columns: AddressId and UserID and a table called AddressCompanies that has the columns AddressId and CompanyId. 在您的情况下,您将有一个名为AddressUsers的表,该表有两列:AddressId和UserID,还有一个名为AddressCompanies的表,其列为AddressId和CompanyId。

Go many-to-many: 多对多:

An address table, with a unique synthetic id (eg, an autoincrement). 地址表,具有唯一的合成ID(例如,自动递增)。

A user_address table, with a unique synthetic id (eg, an autoincrement), a user_id foreign key, and an address foreign key. 一个具有唯一的合成ID(例如,自动递增),一个user_id外键和一个地址外键的user_address表。

A company_address table, with a unique synthetic id (eg, an autoincrement), a company_id foreign key, and an address foreign key. company_address表,具有唯一的合成ID(例如,自动递增),company_id外键和地址外键。

(Note that if users (or companies) could have only one address, you just have an address_id foreign key in the user table (or teh company table). This is not your use case.) (请注意,如果用户(或公司) 只能有一个地址,你就必须在user表中的ADDRESS_ID外键(或德公司表),这是不是你的使用情况。)

What you have is a polymorphic association. 您拥有的是一个多态关联。 I'm not familiar with linqtosql, but if it supports referential integrity on this type of relationship, then fear not, do whatever maps. 我对linqtosql不熟悉,但是如果它在这种类型的关系上支持引用完整性,那么不要担心,请执行任何映射。

In standard practice, a polymorphic association can usually be overcome by reversing it. 在标准实践中,通常可以通过反转多态关联来克服它。 You should go with one intersection (junction) table each for User and Company to join them to Address. 您应该为用户和公司提供一个交集(交汇处)表,以将它们加入到地址中。 This is similar to a many to many relationship, where each row in the intersection table refers to one User and one Address. 这类似于多对多关系,交集表中的每一行都引用一个用户和一个地址。

If you use the intersection tables, to avoid many-to-many (but keep one-to-many) put a unique constraint on the address key in the intersection tables. 如果使用相交表,为避免多对多(但保持一对多),对相交表中的地址键设置唯一约束。

If you're having ORM issues, use a parent address table that joins with UserAddress and CompanyAddress. 如果您遇到ORM问题,请使用与UserAddress和CompanyAddress联接的父地址表。

I recommend following the advice of tpdi, but use a one to many relation between users/companies and addresses; 我建议遵循tpdi的建议,但要在用户/公司与地址之间使用一对多关系; at least then all your key data types are the same. 至少您的所有关键数据类型都相同。

The main reason for adding my answer is in response your second suggestion of storing an ObjectID and an ObjectType - attribute splitting is a bad idea, avoid! 添加我的答案的主要原因是为了响应您关于存储ObjectID和ObjectType的第二建议-属性拆分是个坏主意,请避免!

Have a read of this Celko post: http://www.tdan.com/view-featured-columns/9852 阅读以下Celko帖子: http ://www.tdan.com/view-featured-columns/9852

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM