简体   繁体   English

如何使用流畅的API在MVC实体框架C#中编写(第一个)以下三元关系?

[英]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. 新例子: 狗品种 - 疾病 - 医学。

Description 描述

  • 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 一种药对一种或多种治疗某种疾病的狗有效

    • Thus, a medicine has a list of pairs which is necesary and, and though each pair should be unique, there is not a real relation key value because both values can be repeated. 因此,药物具有必需的对的列表,并且尽管每对应该是唯一的,但是没有真正的关系键值,因为两个值都可以重复。

Code

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; }
}

DogBreed与疾病之间存在多种关系

The problem 问题

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. 我不是在寻找如何绕过这个问题的建议。


Old explanation 旧解释

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. 实体:学生,科目,课堂。

  • 1 Student has several subjects. 1学生有几门科目。
  • 1 Subject is attended by several Students. 1名学生参加了1个科目。
  • 1 Classroom can hold lessons for several subjects as well as be related to all those students which attend the classes so we would have the following diagram 1个课堂可以为几个科目提供课程,也可以与所有参加课程的学生相关,这样我们就可以得到以下图表

关系图

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,并自动关联多对多关系。 表EF仅针对两个实体之间的多对多关系自动生成

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;
}
  • Any other configuration of the database gets duplicate data. 数据库的任何其他配置都会获得重复数据。
  • A dictionary value does not work (plus, it is not correct as it does not match the real meaning of the entities for there is no real key-value relation in real world between theese three) 字典值不起作用(加上,它不正确,因为它与实体的真实含义不匹配,因为在三者之间的现实世界中没有真正的键值关系)
  • In my real problem a student would have a list of subjects, and a subject a list of students. 在我真正的问题中,学生将有一个主题列表,一个主题列出学生。 Then separately I would define Classroom and which would be identified by a unique combination of student-subject. 然后我将单独定义课堂,并通过学生主题的独特组合来识别。

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原则。

  • I am using Entity Framework 6 on windows and MySql Server. 我在Windows和MySql Server上使用Entity Framework 6。

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.

相关问题 c# - 如何通过实体框架代码的流畅API首先在c#中更改表名? - how can i change table name by fluent API of entity framework code first in c#? 用于一对一实体关系的Entity Framework 6代码第一个流利的api配置 - Entity Framework 6 code first fluent api config for one to one entity relationship 如何使用 Entity Framework Code First Fluent API 指定表名 - How to specify table name with Entity Framework Code First Fluent API 实体框架代码第一流畅的api - entity framework code first fluent api 实体框架代码First Fluent API配置,用于一对一的识别关系 - Entity Framework Code First Fluent API configuration for one to one identifying relationship 如何在MVC4和实体代码中首先在Fluent中编写多对多关系 - How to write a many-to-many relationship in Fluent in MVC4 and Entity Code First 在代码中加入表第一个实体框架MVC c# - trouble joining tables in code first entity framework MVC c# 如何首先使用流畅的api代码创建循环引用 - How create circular reference with fluent api code first c# 如何使用实体框架流利的API配置多对多关系 - How to configure many to many relationship using entity framework fluent API 没有流利的API的C#多对多关系代码 - c# many-to-many relationship code first without fluent API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM