简体   繁体   English

处理多对多关系的最干净方法是什么?

[英]What is the cleanest approach handling many to many relationships?

A little background on this question as I know most commonly using an assignment/junction table will address this... 我最常使用分配/连接表了解此问题的一些背景知识可以解决此问题...

I'm working on a C# MVC web application, and I have classes a through f. 我正在使用C#MVC Web应用程序,并且具有类a至f。 class a is a base class for classes b through g all have multiple properties in common. a类是b到g类的基类,它们具有多个共同的属性。 My issue lies in classes b and c have a many to many relation ship as well as b/d, e/c, and f/d. 我的问题在于类b和c具有多对多关系以及b / d,e / c和f / d。

I've looked through inheritance models, but I've not really found an example where subclasses have many to many relationships or how best to address them. 我已经研究了继承模型,但实际上并没有找到一个示例,其中子类具有多对多关系或如何最好地解决它们。

It looks like my options are either: A) Table/Class A as my base, with tables for the assignments b/c, b/d, e/c, and f/d or B)Throw the Base out the window and have tables b, c, d, e, f and still have the tables for the assignments b/c, b/d, e/c, and f/d 看来我的选择是:A)将表/ A类作为我的基础,并使用分配表b / c,b / d,e / c和f / d或B)将“基础”扔出窗外,表b,c,d,e,f仍然具有分配表b / c,b / d,e / c和f / d

My question is am I missing an option, is there a preferred direction on this? 我的问题是我是否缺少一个选择,是否有首选的方向?

UPDATE: I'm looking for the best approach in both the DB and the application structure. 更新:我正在寻找数据库和应用程序结构中的最佳方法。 Does the underlying table structure need to follow option A or B. I'm sorry if this question seems silly, but I've been beating my head on this one. 基础表结构是否需要遵循选项A或B。如果这个问题看起来很愚蠢,对不起,但是我一直在想这个问题。

Why Inheritance ... rather you should be using Composition here. 为什么继承 ...而是您应该在这里使用Composition I mean your class b should be composed with class c instance as class member. 我的意思是您的班级b应该与班级c实例一起构成班级成员。 something like 就像是

public class B
{
  public IEnumerable<C> ListOfC { get; set; }
} 

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

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