实体框架导航属性生成规则 [英] Entity Framework Navigation Property generation rules

查看:124
本文介绍了实体框架导航属性生成规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道实体框架关于导航属性的命名/生成的规则。我已经观察到几个似乎没有意义的场景,所以我想知道有没有人知道这些工作是如何的。

I would like to know what rules Entity Framework follows in regards to the naming/generation of navigation properties. I have observed several scenarios which don't seem to make sense so I was wondering if anyone knows exactly how these work.

场景1:

public class Post
{
    public int Id { get; set; }
    public User Author { get; set; }
} 

   生成

   Generates

即。默认情况下,导航属性生成名为[PropertyName] _Id

ie. by default navigation properties generate FKs named [PropertyName]_Id

方案2:

如果EF手动指定FK ID,EF会生成格式为[PropertyName] _Id的属性,那么它将遵循相同的规则:

It makes sense that if EF generates properties such of the format [PropertyName]_Id when you manually specify a FK Id it will follow the same rules however:

public class Post
{
    public int Id { get; set; }
    public int? Author_Id { get; set; }
    public User Author { get; set; }
}

   生成

   Generates

如您所见,这并不会自动注册为导航资源。

As you can see this doesn't automatically register as a nav property.

方案3:

如果它不适用于情景2,为什么它可以用于替代命名约定?

If it doesn't work for Scenario 2 why does it work for an alternate naming convention?

public class Post
{
    public int Id { get; set; }
    public int? AuthorId { get; set; }
    public User Author { get; set; }
}

   生成

   Generates

导航属性检测和生成的规则是什么?

What are the rules around navigation property detection and generation?

推荐答案

这是预期的行为,它基于两种不同的约定EF

That is expected behavior and it is based on two different conventions based by EF


  • 在第一个示例中,您使用独立关联,您的实体不具有FK属性。 EF将使用简单的模式在数据库中创建FK: NameOfNavigationProperty_NameOfRelatedPK 此惯例遵循传统的数据库命名。

  • 在第二个示例中,您定义了属性与EF使用的FK名称相同。 EF检测到这一点,并在其生成的FK中加1。您的属性不被用作FK的原因是搜索FK属性的第二个惯例。这个约定期望FK属性将具有这个名称(约定遵循传统的.NET命名):

    • NameOfNavigationPropertyNameOfRelatedPK code> NavigationPropertyNameForeignKeyDiscoveryConvention

    • NameOfRelatedTypeNameOfItsPK TypeNameForeignKeyDiscoveryConvention提供

    • NameOfRelatedPK 提供PrimaryKeyNameForeignKeyDiscoveryConvention

    • In the first example you are using Independent association where your entity doesn't have FK property. EF will create FK in the database using simple pattern: NameOfNavigationProperty_NameOfRelatedPK This convention follows traditional database naming.
    • In the second example you defined property with the same name as FK used by EF. EF detected this and added 1 to its generated FK. The reason why your property is not used as FK is the second convention which searches for FK properties. This convention expects that FK property will have this name (conventions follows traditional .NET naming):
      • NameOfNavigationPropertyNameOfRelatedPK provided by NavigationPropertyNameForeignKeyDiscoveryConvention
      • NameOfRelatedTypeNameOfItsPK provided by TypeNameForeignKeyDiscoveryConvention
      • NameOfRelatedPK provided by PrimaryKeyNameForeignKeyDiscoveryConvention

      这篇关于实体框架导航属性生成规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆