Search code examples
c#entity-frameworkforeign-keysentity-framework-6navigation-properties

Navigational Properties naming in EF 6


I have multiple fields in a table referencing the primary key of one table:

  1. Users
    1. CountryId int
    2. CurrencyCountryId int
    3. MobileCarrierCountryId int
  2. Country
    1. Id int

When I import this structure in EF, it scaffolds the Navigation Property for CurrencyCountry and MobileCarrierCountry as Country1 and Country2 respectively, although I used a proper naming convention for foreign keys in the database as FK_User_Country_CurrencyCountry and FK_User_Country_MobileCarrierCountry.

I tried to name the Foreign Keys as FK_User_CurrencyCountry and FK_User_MobileCarrierCountry but it still generates Country1 and Country2 only.

One solution was renaming the navigation properties manually in the edmx file, but I don't know if that's efficient--after I renamed the navigation properties to what I want deleted one of the Models and re-generated, to check if the navigation properties of User table reverts back to default, but it doesn't.


Solution

  • I've had the same thing happen. I just renamed the properties in the edmx file and never had a problem.

    I wasn't aware that the name of the FK could affect the name of the navigation property. I have on occasion given an FK a "non-standard" name but I've never seen the nav property have any other name than the target table (plus sequence number when needed to make the name unique).