简体   繁体   English

实体框架中的外键

[英]Foreign Key in Entity Framework

I am reading through this tutorial.... ASP.NET Code First MVC Tutorial 我正在阅读本教程。... ASP.NET代码优先MVC教程

In this example, Student Class has the "ID" property. 在此示例中,学生班级具有“ ID”属性。

Enrollment Class has "StudentID" property. 注册类具有“ StudentID”属性。

Now, If all the additional definition in Enrollment class says, 现在,如果“注册”类中的所有其他定义都说,

                public virtual Student Student { get; set; }

Just by that statement, will the EF know that StudentID is foreign key? 仅仅通过该声明,EF就能知道StudentID是外键吗?

My confusion is, in the Student Table, property name is "ID" .. Say in the Enrollment Class, instead of StudentID, I call it.... Student_ID....will the EF still correlate Student.ID as the same as Enrollment.Student_ID and establish foreign key? 我的困惑是,在“学生表”中,属性名称是“ ID” ..在注册课程中说,我将其命名为StudentID,而不是StudentID。作为Enrollment.Student_ID并建立外键? Does it just need the "Student" string in a property name in Enrollment class to establish relationship? 是否仅需要Enrollment类中属性名称中的“ Student”字符串来建立关系?

Code snippets from the site... 网站上的代码段...

       using System;
       using System.Collections.Generic;

       namespace ContosoUniversity.Models
       {
           public class Student
           {
               public int ID { get; set; }
               public string LastName { get; set; }
               public string FirstMidName { get; set; }
               public DateTime EnrollmentDate { get; set; }

               public virtual ICollection<Enrollment> Enrollments { get; set; }
           }
       }



       namespace ContosoUniversity.Models
       {
           public enum Grade
           {
               A, B, C, D, F
           }

           public class Enrollment
           {
               public int EnrollmentID { get; set; }
               public int CourseID { get; set; }
               public int StudentID { get; set; }
               public Grade? Grade { get; set; }

               public virtual Course Course { get; set; }
               public virtual Student Student { get; set; }
           }
       }

Entity Framework follows a "Convention over configuration" approach with regards to Foreign Keys. 实体框架对外键遵循“配置公约”方法。

"Any property with the same data type as the principal primary key property and with a name that follows one of the following formats represents a foreign key for the relationship: '[navigation property name][principal primary key property name]', '[principal class name][primary key property name]', or '[principal primary key property name]'. If multiple matches are found then precedence is given in the order listed above. Foreign key detection is not case sensitive. " “具有与主主键属性相同的数据类型,并且名称遵循以下格式之一的任何属性均表示该关系的外键:'[导航属性名称] [主要主键属性名称]','[ “主类名称] [主键属性名称]”或“ [主主键属性名称]”。如果找到多个匹配项,则按上面列出的顺序给出优先级。外键检测不区分大小写。

Source: http://msdn.microsoft.com/en-us/data/jj679962.aspx 来源: http//msdn.microsoft.com/en-us/data/jj679962.aspx

So as long as you follow one of the conventions then it will infer the Foreign Key for you. 因此,只要您遵循其中一种约定,它将为您推断外键。 If you don't follow one of the conventions, then you will have to explicitly state the relationship through a configuration. 如果您不遵循任何一种约定,那么您将必须通过配置明确声明该关系。

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

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