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.

Saman Gholami
  • 3,416
  • 7
  • 30
  • 71
  • Would you tell me how you create model from database? Do you create 'LinQ to SQL' or 'Entity Framewok'? – Lali Sep 02 '15 at 07:49
  • @Lali entity-framework tag is assigned to this question, so I'm using that. – Saman Gholami Sep 02 '15 at 08:14
  • 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. – Lali Sep 02 '15 at 08:34
  • @Lali 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. – Saman Gholami Sep 02 '15 at 08:46

1 Answers1

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.

Lali
  • 2,816
  • 4
  • 30
  • 47
  • 5
    Although the problem is not solved, this is the only solution that could make the update process a little bit easier, thanks. – Saman Gholami Sep 02 '15 at 09:33