Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Default Name of Relations

I have two tables with below structure :

*Table User*

id       int      identity
username varchar
role     varchar


*Table Ticket*

id       int      identity
admin_id int
user_id  int

admin_id and user_id are the foreign keys from User table. I set a relation name for the relations in Mssql diagram, but when i create my model from this database, the relations are : User and User1. When I'm changing them manually, it is going Ok, but after refreshing the model or re-create the Ticket table, User and User1 are back. How can i set the final name for my relations in EF model?even i refresh or delete the entire model, i want to the EF Model relation name are being same as the diagram relation name.

like image 222
Saman Gholami Avatar asked Aug 17 '14 20:08

Saman Gholami


1 Answers

There is no way to force Entity Framework to get names of properties from relation names, instead it takes table name as property name (with integer suffix for multiple properties).

Now the issue, in your scenario is that your renamed properties change to their original names when ever you update model from database.

We can stop entity framework to do not reset property names which we renamed them by our own. The way is,

  1. Open model file (.edmx)
  2. Go to Association folder in model browser
  3. Right click on the association (Which you want to rename) & go to properties
  4. Check the Property pallet, start searching End2 Navigation Property from bottom.
  5. Rename it whatever you want.

that is done.

Now, it will not be refreshed even you update model from database.

like image 177
Lali Avatar answered Oct 15 '22 22:10

Lali