简体   繁体   English

没有流利的API的C#多对多关系代码

[英]c# many-to-many relationship code first without fluent API

I'm trying to understand Entity Framework Code First. 我试图首先了解实体框架代码。

Is it possible to configure a many-to-many relationship without using fluent API but just DataAnnotation? 是否可以不使用流利的API而仅使用DataAnnotation来配置多对多关系? For example in this tutorial I don't understand if the two examples are the same thing but done in different ways or if they have to exist together. 例如,在本教程中,我不了解这两个示例是否是同一件事,但是以不同的方式完成,或者它们是否必须并存。

  • If they are different ways for the same thing, what is actually the difference? 如果在同一件事上使用不同的方式,那么实际上有什么区别?

  • And if they have to coexist, what is the API "part" doing? 如果必须共存,API“部分”在做什么?

Indeed you are right. 确实你是对的。

This paragraph: 本段:

Configure Many-to-Many relationship using DataAnnotation: 使用DataAnnotation配置多对多关系:

Student class should have a collection navigation property for Course, and Course should have a collection navigation property for student, 学生班级应具有课程的集合导航属性,课程应具有学生的集合导航属性,

is wrong. 是错的。 Because the many-to-many relationship in the sample code in created automatically by convention when you add a collection in each class. 因为在每个类中添加一个集合时,示例代码中的多对多关系都是按约定自动创建的。 The only data annotation that is used is Required attribute which is not implied in the many-to-many relationship. 使用的唯一数据注释是“ Required属性,该属性在多对多关系中没有隐含。

BTW the section about "Configure Many-to-Many relationship using Fluent API" is correct but in their sample code they use it only put a custom name to the foreign key in the join table instead of letting Code First generating it. 顺便说一句,有关“使用Fluent API配置多对多关系”的部分是正确的,但是在他们的示例代码中,他们仅使用自定义名称为联接表中的外键添加名称,而不是让Code First生成该名称。 Foreing key renaming can be done too with Data Annotation by using ForeignKey attribute. 也可以通过使用ForeignKey属性使用数据注释来进行前键重命名。

They're two different ways of configuring the database. 它们是配置数据库的两种不同方式。 They can coexist, but they don't have to; 它们可以共存,但是不必共存。 you can use just one or the other. 您可以只使用其中一个。 If you do use both, the Fluent API takes precedence over the data annotations, but the data annotations still take precedence over the default conventions. 如果两者都使用,则Fluent API优先于数据注释,但是数据注释仍优先于默认约定。

The difference is that API allows a more exacting degree of control, in exchange for being (arguably) more complicated. 区别在于,API允许更精确的控制程度,以换来(可以说)更复杂。 The example you linked does show two different ways of accomplishing effectively the same thing, and you could use one or the other if you wanted to create that specific many-to-many relationship. 您链接的示例确实显示了有效完成同一件事的两种不同方法,如果您想创建特定的多对多关系,则可以使用其中一种。 But the API method would be more configurable if you wanted to do something odd with that CourseStudents (or StudentCourse) table, while the annotation method would be more limited but is easier to read at a glance if you don't need the extra functionality. 但是,如果您想对CourseStudents(或StudentCourse)表做一些奇怪的事情,API方法将更具可配置性,而注解方法将受到更多的限制,但如果您不需要额外的功能,则一目了然。

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

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