Change Default Name of Relations
Asked Answered
C

1

8

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.

Char answered 17/8, 2014 at 20:46 Comment(4)
Would you tell me how you create model from database? Do you create 'LinQ to SQL' or 'Entity Framewok'?Cassondracassoulet
@Cassondracassoulet entity-framework tag is assigned to this question, so I'm using that.Char
I tried to create the same scenario and found that in designer model, there is no explicit mapping for relations. However in Ticket model, there are two properties of type User, named 'user' and 'user1'. I think you are talking about these properties.Cassondracassoulet
@Cassondracassoulet Yes, I'm talking about them, I don't know why these names and the relation names in the database are not the same! It's caused a problem whenever I update the model.Char
C
7

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.

Cassondracassoulet answered 2/9, 2015 at 9:3 Comment(1)
Although the problem is not solved, this is the only solution that could make the update process a little bit easier, thanks.Char

© 2022 - 2024 — McMap. All rights reserved.