MVC 中的主键/外键命名约定

Primary Key / Foreign Key naming convention in MVC

关于 Primary KeyForeign Key 命名约定在 MVC 中有一些不同的方法,我想澄清一下哪一个最有可能接近标准约定,即使它们之间几乎没有区别。下面是使用两个 table 的示例代码,分别称为 StudentCountry.

约定 I: 我更喜欢这种方法,因为我不想在每个 PK 属性 上重复 table 名称。

学生Table

ID => Primary Key property
CountryId => Foreign Key property

国家/地区Table

ID => Primary Key property
// other properties


公约二:

学生Table

StudentId => Primary Key property
CountryId => Foreign Key property

国家/地区Table

CountryId => Primary Key property
// other properties


除了这个问题,我还想知道在 Entity Class 中使用 IDId 作为 Primary Key 是否有区别。有什么想法吗?

这注定会因为自以为是而被关闭,但我会接受第一个约定。这比任何事情都更让我讨厌,但当我看到像这样的东西时,它让我无休止地烦恼:

public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    ...
}

嗯,当然,就是学生的id和学生的名字。那是 class 的名字。然后,你的 API 看起来像 student.StudentName,读起来很可笑。

总而言之,如果 属性 是 class 固有的,则不要在其前面加上 class 名称。你应该有一个 API 像 student.Idstudent.Name.

当谈到外键时,您 应该 在它前面加上 class 名称,因为它告诉您这种关系。 student.CountryId 之类的东西非常清楚地表明这是学生所在国家/地区的外键。在编程中,可读性和自我记录 APIs 是最重要的。