Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 730
Adrian K. Avatar asked Nov 05 '14 13:11

Adrian K.


People also ask

How does Entity Framework handle many-to-many relationships?

To configure many-to-many relationship Using Data Annotations, you need to create the Join Table in the model. The Join Table BookCategory will have properties for the primary key of both the table. It will have two navigational properties one each for Book and Category class.

How will you create relationship between tables in Entity Framework?

You can create such a relationship by defining a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B.

What is ICollection in Entity Framework?

ICollection represents a type of collection. specifying a collection as an ICollection allows you to use any type of collection in your code that implements the ICollection interface.


1 Answers

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)

like image 200
Stewart_R Avatar answered Sep 19 '22 17:09

Stewart_R