简体   繁体   English

实体框架核心外键默认约定

[英]Entity Framework Core ForeignKey Default Conventions

I am looking for a document of [ForeignKey] conventions.我正在寻找[ForeignKey]约定的文档。 I found a list of naming conventions on MSDN .我在MSDN上找到了一份命名约定列表。

I am not sure which convention maps the below action.我不确定哪个约定映射了以下操作。

class Student
{
    public int id{ set; get;}

    public int myclassroomid{ set; get;}

    public Classroom myclassroom{ set; get;}
}

class Classroom
{
    [Key]
    public int cid{ set; get;}
}

I don't understand how the default convention works.我不明白默认约定是如何工作的。

I found the NavigationPropertyNameForeignKeyDiscoveryConvention , It does not seem to apply this scenario.我找到了NavigationPropertyNameForeignKeyDiscoveryConvention ,它似乎不适用于这种情况。

where can I found the official document?我在哪里可以找到官方文档? explain some main points if it has, it's would be better with a code example.如果有的话,解释一些要点,最好有代码示例。

The NavigationPropertyNameForeignKeyDiscoveryConvention covers how the navigation property in Student finds the FK declared in Student. NavigationPropertyNameForeignKeyDiscoveryConvention涵盖了 Student 中的导航属性如何找到 Student 中声明的 FK。 AFAIK it wouldn't consider anything set up in the Classroom entity. AFAIK 它不会考虑在 Classroom 实体中设置的任何内容。

It will use the Type and reference name, so it would look for ClassroomId or MyClassroomId.它将使用类型和引用名称,因此它将查找 ClassroomId 或 MyClassroomId。 This often trips up people which want to use multiple references to the same entity with one navigation property matching the type name.这通常会让那些想要使用多个引用到同一实体的人绊倒,其中一个导航属性与类型名称匹配。 Ie CustomerId/Customer Customer and CreatedById / Customer CreatedBy.即 CustomerId/Customer Customer 和 CreatedById/Customer CreatedBy。 EF may attempt to resolve CreatedBy to CustomerId based on the type. EF 可能会尝试根据类型将 CreatedBy 解析为 CustomerId。

The navigation property itself will marry up the reference to whatever field is declared as the PK in your Classroom type.导航属性本身将结合对在您的课堂类型中声明为 PK 的任何字段的引用。 Removing the [Key] attribute on cId in Classroom wouldn't work since that doesn't match the PK convention rules.删除 Classroom 中 cId 上的[Key]属性将不起作用,因为这与 PK 约定规则不匹配。 (Id or ClassroomId) It also doesn't factor into FKs that way, Ie placing a cId column in Student most likely wouldn't map through to the MyClassroom reference. (Id 或 ClassroomId)它也不会以这种方式影响 FK,即在 Student 中放置 cId 列很可能不会映射到 MyClassroom 引用。

Personally I always use explicit mapping for relationships, and I avoid declaring FK properties, favoring shadow properties/ Map() instead.就我个人而言,我总是对关系使用显式映射,并且我避免声明 FK 属性,而是使用阴影属性/ Map()代替。 If you're going to use conventions then your schema should be set up to conform to them without special cases.如果您打算使用约定,那么您的架构应该设置为符合它们而没有特殊情况。 The easiest and most readable is to use {TableName}+Id for both the PK and FK references.最简单且最易读的方法是对 PK 和 FK 引用使用 {TableName}+Id。 The exception being multi-references to the same table/Entity, then using descriptive names for the references and their associated FKs.例外是对同一个表/实体的多重引用,然后对引用及其关联的 FK 使用描述性名称。 Ie CreatedBy + CreatedById and OrderingCustomer + OrderingCustomerId instead of Customer + CustomerId if both navigation properties were to point at a Customer entity.即如果两个导​​航属性都指向客户实体,则 CreatedBy + CreatedById 和 OrderingCustomer + OrderingCustomerId 而不是 Customer + CustomerId。

You are looking at the wrong places - all these are for EF6, while EF Core is completely different system, so the starting point should be the official documentation here Entity Framework Core .你看错了地方——所有这些都是针对 EF6 的,而 EF Core 是完全不同的系统,所以起点应该是这里的官方文档Entity Framework Core

Foregn Key conventions are described in Relationships - Conventions , in particular:外键约定在关系 -约定中进行了描述,特别是:

If the dependent entity contains a property with a name matching one of these patterns then it will be configured as the foreign key:如果依赖实体包含一个名称与这些模式之一匹配的属性,那么它将被配置为外键:

  • <navigation property name><principal key property name>
  • <navigation property name>Id
  • <principal entity name><principal key property name>
  • <principal entity name>Id

In your example, myclassroomid is matching the rule #2 - navigation property name "myclassroom" + "Id".在您的示例中, myclassroomid匹配规则 #2 - 导航属性名称“myclassroom”+“Id”。 It's not mentioned explicitly but the matching is case insensitive, so "Id` also matches "ID", "id" etc.它没有明确提及,但匹配不区分大小写,因此“Id` 也匹配“ID”、“id”等。

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

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