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.

Karolis Juodelė
  • 3,708
  • 1
  • 19
  • 32
  • There is no edmx in EF5. Are you talking about EF4? – zs2020 Jul 22 '13 at 19:41
  • @sza, that's odd, it claims to be Version 5 in Reference Properties. What I actually did was an "ADO.NET Entity Data Model". – Karolis Juodelė Jul 22 '13 at 20:10
  • I think this thread has a more detailed explanation of what to alter in the EF 5.x T4 templates to achieve this. http://stackoverflow.com/questions/12937193/improve-navigation-property-names-when-reverse-engineering-a-database – Bostrong Dec 18 '13 at 04:26
  • 2
    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? Thanks – Aamir Jul 25 '15 at 10:48
  • 1
    I made a basic .net console app for this https://github.com/timabell/ef-edmx-nav-namer – Tim Abell Aug 13 '15 at 15:52
  • very similar http://stackoverflow.com/questions/7583352/problem-with-navigation-property-naming-convention-in-edmx – Tim Abell Aug 13 '15 at 15:52
  • try using this solution [Define Rules to name Navigational Property](http://stackoverflow.com/questions/12937193/improve-navigation-property-names-when-reverse-engineering-a-database) – Ashutosh Singh Dec 03 '15 at 16:22

1 Answers1

1

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.

Amin Saqi
  • 18,549
  • 7
  • 50
  • 70
  • 5
    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. – Karolis Juodelė Jul 26 '13 at 11:05
  • 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?! – Amin Saqi Jul 26 '13 at 11:09
  • 4
    `OnModelCreating` is only for Code First, isn't it? I need a solution using Database First. – Karolis Juodelė Jul 26 '13 at 12:23
  • 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... – Amin Saqi Jul 26 '13 at 14:56
  • 1
    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... – Karolis Juodelė Jul 26 '13 at 17:26
  • 1
    @KarolisJuodelė you should have shared what you have found! – curiousBoy Feb 13 '19 at 06:55