Entity Framework Navigation Property generation rules

前端 未结 2 688
有刺的猬
有刺的猬 2020-12-05 20:16

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

相关标签:
2条回答
  • 2020-12-05 20:34

    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.
    0 讨论(0)
  • 2020-12-05 20:40

    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

    0 讨论(0)
提交回复
热议问题