13

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.

Scenario 1:

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

   Generates

Scenario 1

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

Scenario 2:

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

Scenario 2

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

Scenario 3:

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

Scenario 3

What are the rules around navigation property detection and generation?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
undefined
  • 33,537
  • 22
  • 129
  • 198
  • Just thought I'd link back to your good [blog post](http://blog.staticvoid.co.nz/2012/7/17/entity_framework-navigation_property_basics_with_code_first) about Navigation Properties. – orad Aug 15 '14 at 21:45
  • @orad lol cool, i actually asked this question as part of the research for that post :) – undefined Aug 15 '14 at 22:58

2 Answers2

13

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

  • 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
  • In the last example you correctly defined FK property and EF detected it so it uses Foreign key association.
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • [Here](https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key) is some document regarding relationships and conventions. – Yoshihiro Tanaka Apr 15 '22 at 17:24
2

In addition to @Ladislav Mrnka's answer above, you can find a detailed reference of the entity framework default conventions here: http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.conventions(v=vs.103).aspx