简体   繁体   English

来自多个不同表的EF多对多关系

[英]EF many-to-many relationships from multiple different tables

Let's say I have 4 tables: tbl_dogs , tbl_cats , tbl_birds and tbl_fish each with their own _Id columns, of course.. I want to create the ability to have many-to-many relationships between any of these tables. 假设我有4个表: tbl_dogstbl_catstbl_birdstbl_fish都有各自的_Id列。.我想创建一种在这些表之间建立多对多关系的功能。 In my head, I picture a tbl_relationships table that has 2 columns: animal1_id and animal2_id and I can entries can be cat_12 | dog_3 在我的脑海中,我描绘了一个tbl_relationships表,该表具有2列: animal1_idanimal2_id ,我可以输入的内容可以是cat_12 | dog_3 cat_12 | dog_3 , bird_1 | dog_9 cat_12 | dog_3bird_1 | dog_9 bird_1 | dog_9 , fish_8 | cat_4 bird_1 | dog_9fish_8 | cat_4 fish_8 | cat_4 , etc. This is a 2 part question: fish_8 | cat_4等。这是一个两部分的问题:

  • 1) is this possible with EF code first? 1)可以先使用EF代码吗? meaning the two columns on my "relationships" table can actually be pulled from multiple different tables? 意味着我的“关系”表上的两列实际上可以从多个不同的表中提取? If so, how would I define that in the EF classes? 如果是这样,我将如何在EF类中对其进行定义?
  • 2) what if rather than animal1_id and animal2_id as the columns, I wanted to have parent_animal and child_animal so that when I went to look at a fish , I could pull all the child_animal records that have that fish as a parent_animal as well as all the parent_animal records that have that fish as a child_animal ? 2)如果我不想使用animal1_idanimal2_id作为列,我想拥有parent_animalchild_animal这样当我去看一条fish ,我可以提取所有以child_animal fishparent_animalchild_animal记录以及所有parent_animal记载,有fish作为child_animal

Any help would be greatly appreciated, thank you! 任何帮助将不胜感激,谢谢!

It's not possible with any relational database. 任何关系数据库都不可能。 Entity Framework is beside the point. 实体框架不重要。 A foreign key has to be made to a specific table. 必须为特定表创建外键。 Period. 期。

The Python framework, Django does sort of what you're looking for with its generic content types, but it's more of a hack than anything. Django是Python框架,具有通用的内容类型,可以满足您的需求,但它比其他任何东西都更适合您。 For each generically related item, two columns are set: one for the type of the object and one for the id of the object. 对于每个一般相关的项目,设置两列:一列用于对象的类型,一列用于对象的id。 There are no foreign keys, because again, foreign keys are impossible in this scenario. 没有外键,因为同样在这种情况下外键是不可能的。

In the framework code, in order to materialize the related object, they then issue a query to the correct table (based on the object type) using the object id. 在框架代码中,为了实现相关对象,他们随后使用对象ID向正确的表(基于对象类型)发出查询。 However, this is much simpler in Python than C# because Python is duck-typed. 然而,这在Python比C#简单得多 ,因为Python是鸭类型。

You can technically achieve the same thing, in C# if you were properly motivated, but it would be an entirely manually endeavor. 如果您有适当的动机,则可以在C#中从技术上实现相同的目标,但这将是完全手动的工作。 Entity Framework is of no help to you here. 在这里,实体框架对您没有帮助。 You would also need to employ reflection in order to materialize the right type in the end, and reflection is both a pain in the posterior and hugely inefficient (slow). 您还需要使用反射来最终实现正确的类型,反射既是后部的痛苦,又是效率极低的(缓慢)。

That said, since the specific scenario here deals with things all of a certain type, animals, you can sort of achieve what you're looking for with inheritance. 就是说,由于这里的特定场景处理的是某种特定类型的事物(动物),因此您可以通过继承来实现所需的内容。 In other words, you can create an Animal entity and then have each of Dog , Cat , Bird , Fish , etc. inherit from Animal . 换句话说,您可以创建一个Animal实体,然后让DogCatBirdFish等等每个继承自Animal Then, you can create a foreign key to Animal and interact with any of them. 然后,您可以创建Animal的外键并与其中任何一个进行交互。 However, you would only be able to interact with them as Animal s, not as specifically a Bird , for example. 但是,您将只能与Animal互动,例如,不能与Bird互动。

If you need to have a single matrix table with just two columns, and you need each animal type to be in its own table, then Chris Pratt's answer is succinct. 如果您需要一个只有两列的矩阵表,并且需要将每种动物类型都放在自己的表中,那么克里斯·普拉特的答案很简洁。

If you can go with more than two columns but are forced to stick with individual animal tables, I would try to have a column for every animal table in the matrix table and turn this matrix table into a massive multi-columned monster. 如果您可以使用多于两列的列,但被迫坚持使用单独的动物表,那么我将尝试为矩阵表中的每个动物表创建一列,然后将此矩阵表变成庞大的多列怪物。

If you are limited to two columns in the matrix table but can target a matrix table based on what parent/child combo you are looking for, then I would set up twelve matrix tables and have each of them for one of the two possible animal combinations for each pair of animal tables. 如果您限于矩阵表中的两列,但可以根据要查找的父/子组合确定矩阵表的目标,那么我将设置十二个矩阵表,并将它们分别用于两种可能的动物组合之一每对动物桌子。 Obviously you are going to have issues if the number of animal tables is actually much larger. 显然,如果动物表的数量实际上要大得多,您将遇到问题。

If the animal tables will all have identical columns (type/content/requirements) themselves, there is a possible workaround. 如果动物表本身都具有相同的列(类型/内容/要求),则可能存在解决方法。 You can have all animals in one table with an identifier that signifies what kind of animal it is. 您可以将一个表中的所有动物都带有一个标识符,该标识符表示它是哪种动物。 This would typically be a foreign key attached to a lookup table of animal types. 这通常是附加在动物类型查询表上的外键。 Then you can have your matrix table as both parent and child columns, as they will have explicit relationships to just a single other table (the 'mass animal' table; both parent and child would point to its PK). 然后,您可以将矩阵表同时用作父列和子列,因为它们将与其他单个表(“大众动物”表;父子都指向其PK)具有显式关系。

A more elegant possible workaround to the prior paragraph (which I haven't had a chance to use yet, so I am really hazy on its implementation with respects to gotchas) is Inheritance . 继承是上一段的一种更优雅的解决方法(我还没有机会使用它,因此我对实现这一点感到很困惑)。 Inheritance allows you to combine several tables that hold near-identical (in terms of fields and data types) collections of data, and be able to tell those collections apart by means of a discriminator. 继承使您可以合并几个表,这些表保存(在字段和数据类型方面)几乎相同的数据集合,并能够通过区分器将这些集合区分开。 That way, as per the prior paragraph, you could have a single Animals table with a discriminator that indicates what each row is (cat, dog, etc.), and a matrix table where both the parent and child point to the ID of the Animals table. 这样,按照前面的段落,您可以有一个Animals表,该表带有一个鉴别符,用于指示每行的内容(猫,狗等),以及一个矩阵表,其中父级和子级均指向该ID。动物表。 Since each column in the matrix table has an association to a unique table (instead of multiple tables), this could work out well. 由于矩阵表中的每一列都与唯一表(而不是多个表)相关联,因此可以很好地解决这一问题。

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

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