[英]How to code (first) the following ternary relationship in MVC Entity Framework C# with fluent API?
UPDATE UPDATE
It seems I did not make my self understood. 看来我没有让自己明白。 I might have chosen the problem incorrectly.
我可能错误地选择了问题。 So I will try with another example which hopefully will illustrate better my problem.
所以我将尝试另一个例子,希望能够更好地说明我的问题。
New example: Dog breed - disease - medicine. 新例子: 狗品种 - 疾病 - 医学。
A dog breed can be affected by several diseases. 狗的品种可能受到几种疾病的影响。
One same disease can affect several dog breeds. 同一种疾病可以影响几种犬种。
One medicine is effective on one or more disease for a certain dog breed 对于某种狗品种,一种药对一种或多种疾病有效
One medicine is effective for one or more dogs treating a certain disease 一种药对一种或多种治疗某种疾病的狗有效
The following code works correctly with EF creating a lookup table between DogBreed and Disease as shown in the image that follows: 以下代码可以正常使用EF在DogBreed和Disease之间创建查找表,如下图所示:
[Table("dog_breed")]
public DogBreed (){
public int ID { get;set; }
public string DogBreedName { get;set; }
public IList<Disease> Diseases { get; set; }
}
[Table("disease")]
public Disease (){
public int ID { get;set; }
public string DiseaseName { get;set; }
public IList<DogBreed> DogBreeds { get; set; }
}
I want to know how to add the entity medicine to my project as follows and get the EF to generate the scheme showed in the next picture. 我想知道如何将实体药物添加到我的项目中,如下所示,并获得EF生成下图中显示的方案。
The code could be something like this: 代码可能是这样的:
[Table("medicine")]
public Medicine (){
public int ID { get;set; }
public string MedicineName { get; set; }
public Lookup<DogBreed,Disease> { get;set; }
}
But this does not generate the scheme wanted. 但这并不会产生所需的方案。 How should I proceed?
我该怎么办?
I'd like to clarify that my doubt is how to make the code automatically generate the structure I want on my data base so I can use entity framework features seamlessly, therefore all solutions implying creating tables on the database manually, or changing the structure (ie defining a new class) have already been considered. 我想澄清一下,我的疑问是如何使代码自动生成我想要的数据库结构,这样我就可以无缝地使用实体框架功能,因此所有解决方案都意味着手动在数据库上创建表,或者改变结构(已经考虑过,即定义一个新的类。 I'm not looking for proposals on how to bypass the problem.
我不是在寻找如何绕过这个问题的建议。
I have a simple ternary relationship in which three entities relate as I describe below in an example made up for my specific problem. 我有一个简单的三元关系,其中三个实体相关,正如我在下面描述的一个例子中为我的具体问题做了解释。 The problem is I do not know how to type the code, to get the tables in my database as I expect them to be.
问题是我不知道如何键入代码,以获得我期望它们在我的数据库中的表。
Entities: Student, Subject, Classroom. 实体:学生,科目,课堂。
So this could be easily related in a separate table which would relate the three as follows, where student 1, would take lessons from subject 13, in classroom 2. 因此,这可以很容易地在一个单独的表格中相关联,该表格将如下三个相关联,其中学生1将在课堂2中学习主题13的课程。
This is the behaviour I observe for binary relation when coded the following way: 这是我在以下列方式编码时观察到的二元关系的行为:
[Table("student")]
public class Student
{
[Key]
public int ID {get; set;}
public List<Subject> Subjects;
}
[Table("subject")]
public class Subject
{
[Key]
public int ID {get; set;}
public List<Student> Students;
}
In this case, when I run Add-Migration and Update-Database I get a table studentsubject which contains id's for both entities and relates the many to many relationship automatically. 在这种情况下,当我运行Add-Migration和Update-Database时,我得到一个表studentsubject,其中包含两个实体的id,并自动关联多对多关系。
So what would be the way to code the classes if the relationships in as to get that intermediate table with 3 fields, if possible? 那么,如果可能的话,如果要获得具有3个字段的中间表的关系,那么对类进行编码的方法是什么? .
。 Collections, properties, classes dictionaries...?
收藏,属性,班级词典......?
-The real design for my example is adding to the first listing a the following class: - 我的例子的真实设计是在第一个列表中添加以下类:
[Table("classroom")]
public class Classroom
{
[Key]
public int ID {get; set;}
public KeyValuePair<Student, Subject> StudentSubject;
}
For the time being I will create a class which represents that ternary relationship although I feel this doesn't stick to KISS principle. 暂时我将创建一个代表三元关系的课程,虽然我觉得这不符合KISS原则。
There's no need to mention "Students" directly in "Classroom" Object,cause it has a list of "subjects" and "subject" itself has a list of "Student" 没有必要直接在“课堂”对象中提及“学生”,因为它有一个“科目”列表和“科目”本身有一个“学生”列表
this is wrong: 这是错的:
public KeyValuePair<Student, Subject> StudentSubject
;public KeyValuePair<Student, Subject> StudentSubject
;
it means inserting Student to subject once in "Subject" object and once "Again" in Classroom,Why?! 这意味着在“主题”对象中插入一次学生主题一次,在课堂中插入一次“再次”,为什么?!
You must have something like this and EF will do the magic: 你必须有这样的东西,EF会做出神奇的事:
[Table("classroom")] public class Classroom { [Key] public int ID {get; set;} public List<Subject> Subject; }
and it will be your each Classroom's Students for each Subject 每个科目都是您每个课堂的学生
List<Student> classroomSubjectStudents = classroom.Subjects .Where(x=>x == someSubject) .Select(x=>x.Students).ToList();
After searching for a while and waiting for some time I whent back to school and: 经过一段时间的搜索并等待一段时间后,我又回到了学校,并且:
By definition, well normalized relationships are binary relationships. 根据定义,良好规范化的关系是二元关系。 Therefore a ternary relationship as described in the example will not work with code first , nevertheless the nature of the relationship makes the binary relationships that form it (dog breed - medicine, medicine - disease, dog breed - disease) important enough to form entities by their own.
因此,示例中描述的三元关系不会首先与代码一起使用,然而关系的本质使得形成它的二元关系(狗品种 - 医学,医学 - 疾病,狗品种 - 疾病)重要到足以形成实体他们自己的。
Depending on your problem, you should just choose one to form an entity. 根据您的问题,您应该选择一个来形成一个实体。 So the answer to the question would be...
所以这个问题的答案是......
To take advantage of entity framework and code first, in order to code a ternary relationship you must make the relationship between two of the parts an entity itself. 要首先利用实体框架和代码,为了编写三元关系,您必须将两个部分之间的关系作为实体本身。
[Table("dog_breed")]
public DogBreed (){
public int ID { get; set; }
public string DogBreedName { get; set; }
public List<PairMedicineDisease> PairsMedicineDisease { get; set; }
}
[Table("medicine")]
public Medicine (){
public int ID { get;set; }
public string MedicineName { get;set; }
}
[Table("disease")]
public Disease (){
public int ID { get;set; }
public string DiseaseName { get;set; }
}
[Table("pair_medicine_disease")]
public PairMedicineDisease (){
public int ID { get;set; }
public Disease Disease { get;set; }
public Medicine Medicine { get;set; }
}
Thanks to all who tried to help, specially to @ Gert Arnold who pointed me in the right direction. 感谢所有试图提供帮助的人,特别感谢@ Gert Arnold ,他指出了我正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.