Search code examples
c#entity-frameworknaming-conventionsforeign-key-relationshipnavigation-properties

Entity Framework (Database-First) multiple relations to same table naming conventions control


Let's suppose that we have this situation:

Tables in database:

Country (id, country_name), Person (id, login), CountryManager (id_country, id_person), CountryStakeholder (id_country, id_person)

If we had to create the model from the database, using Entity Framework Database-First, in VS we'd have a class like this:

class Country {

int id;
string country_name;

virtual ICollection<Person> Person1; // Navigation Properties
virtual ICollection<Person> Person2; // ---------||----------

}

I've simplified the code a lot, but hopefully you got the point.

Seems that when Entity Framework deals with foreign keys it creates generic Navigation Properties. Is there a possibility to control how Navigation Properties are created by name? Person1, Person2 isn't very explainatory, unfortunately.


Solution

  • In VS you can do this with the GUI.

    If you show Model Browser then navigate down the tree to:

    YourEntityModel > Entity Types > Country

    then right-click on the "Person1" Navigation Property and select "Properties" you can then change the name of the navigation property name to whatever you like:

    enter image description here

    Just change the name, save changes and your done...

    (Actually there are lots of ways to get to the Navigation Property properties window - you cn right click on it in the model diagram too)