Renaming navigation properties
Asked Answered
G

1

8

Sometimes entity framework generates awful navigation property names. Using the database first approach, what is the best way to fix that?

I am able to edit the t4 templates to my liking, but how would I bind these modified names to actual relations? Should I just write a script to modify the edmx file? Obviously, doing it by hand is is hardly an option.

Goodfellowship answered 22/7, 2013 at 18:0 Comment(7)
There is no edmx in EF5. Are you talking about EF4?Niveous
@sza, that's odd, it claims to be Version 5 in Reference Properties. What I actually did was an "ADO.NET Entity Data Model".Aeschylus
I think this thread has a more detailed explanation of what to alter in the EF 5.x T4 templates to achieve this. #12937693Tent
I m using EF version 6, do they have anything to solve this issue? I am also renaming the navigation properties every time I recreate/reload the EDMX file in DB First project. Can u plz share the script that you are using to rename the properties? ThanksTenfold
I made a basic .net console app for this github.com/timabell/ef-edmx-nav-namerHirst
very similar #7583852Hirst
try using this solution Define Rules to name Navigational PropertyCreolacreole
E
2

Yeah, you can. In most of cases you just need to rename that NavPr from the EF model designer and save the model. Then EF renames that NavPr in your DbContext.tt entity classes and it cause no problem.

If you got problems with the ralation name, right-click at the renamed NavPr and select properties. Then change the Association property of it to the actual relation in your database.

If you can't see the relative relation name there, delete that entity and all other entities which has a relation to it, right-click on model designer and choose Update model from database. Then, simply rename that ugly NavPr and save the model.

Educative answered 26/7, 2013 at 3:32 Comment(6)
I don't want to do it in the model designer. Often there are too many tables for that. Also, should there ever be a need to generate a new model, all the changes would be lost. What I'd like is a way to tell EF what naming conventions to use.Aeschylus
You just can remove the naming convention of EF by overriding OnModelCreating in DbContext, and by doing so, you must tell all things about naming properties, foreign keys, etc. to EF...! Which way is simpler?!Educative
OnModelCreating is only for Code First, isn't it? I need a solution using Database First.Aeschylus
So you doesn't have a choice at all. Besides, it is possible to start with Db first, and change the way to Code first... Maybe you also are going this way...Educative
Well, at the moment I've already written a Perl script to fix my .edmx, so I'm fine. I was hoping to find a more elegant solution though...Aeschylus
@KarolisJuodelė you should have shared what you have found!Milliard

© 2022 - 2024 — McMap. All rights reserved.