0

To the point; is there any way to customize the pluralization service for database-first EF models?

Specifically, I'd like to use the *Set suffix notation, wherein the entity sets and collection navigation properties are named accordingly:

  • User to UserSet
  • Report to ReportSet
  • etc.

I know I've seen this made possible with code-first, however I'm stuck with database-first as the development process.

I'm aware of IPluralizationService, but can't figure out how to substitute my custom implementation.

Currently, I'm manually working through the entity sets and collection properties in the model browser (VS2015) and appending "Set" to each of them; this is fine to do once, however whenever I regenerate the model it becomes quite the pain in my ass.

Any suggestions?

Dan Lugg
  • 20,192
  • 19
  • 110
  • 174

3 Answers3

1

You could write something that will update the edmx file to the new names. Also I was going to suggest you could alter the t4 script (the .tt files) but I think that will break the mapping with the edmx file in a database first situation.

But I think you should reconsider code first, you can use the code first generator multiple times, just clean out the context class, and the connection string in the config and make a new context that is named the same (it will overwrite the table classes). You can nuget EntityFramework.CodeTemplates.CSharp and alter the t4 templates that it downloads to include "Set" and that is what it will use to generate the classes.

And then you don't fall into edmx hell, edmx files are a pain once you start trying to maintain them instead of letting them just be what is generated.

johng
  • 171
  • 9
  • Unfortunately, it's not really an option to "reconsider" code-first; I'm stuck with an existing (however, evolving) schema. And yes, I tried altering the T4 scripts, but that does break the mapping. The closest I've gotten is writing a transformation script (I think I used PHP actually) to run after the EDMX is generated, but it's clunky. – Dan Lugg Nov 16 '16 at 19:45
  • I've done a conversion from edmx to code first, not understanding completely why you are stuck? It is tedious though; you go into the project file and un-nest the class files from under the t4 script, delete the t4 scripts. and then work your way though property name by property name renaming it to the code first generated format (and using the lightbulb to fix all code locations). Fix artificial foreign keys by actually adding them to the database, or creating extension methods to call instead. 30 tables took me one day of work. Note: careful of razor (MVC) files, lightbulb won't find them – johng Nov 16 '16 at 20:05
  • Yea, I didn't go through all that; I just wrote a script that read the EDMX XML and appended "Set" to entity sets and collection navigation property names. Then I just re-ran the code generation so the correct names are used when generating classes via the T4. The EDMX is fine because the mapping is still correct. Either way, it's tedious because the script I wrote didn't handle corner cases and I ended up manually editing the EDMX anyway. – Dan Lugg Nov 16 '16 at 20:09
  • Yeah, that's the best way if you're sticking with the edmx. With edmx source control is your friend, its only matter of time before a custom edit ruins it. – johng Nov 16 '16 at 20:18
  • I ended up just further developing the EDMX transformation script (in PHP, funny enough) so it now handles most cases and retains the mapping between conception/storage/etc models. – Dan Lugg May 12 '17 at 02:09
0

I ended up writing a script (PHP of all things) to perform XML transformations on the EDMX file. I lose support for some of the more obscure features due to the way the transformation is performed, however it was the only way I could do it without sacrificing kittens to an omniscient force. Most importantly, it maintains the mappings as expected.

I couldn't figure out a way to work the transformation script into the generation pipeline yet; though I may look at invoking it from the text template.

Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
0

I'm aware that the question and its answers are 4 years old, but:

In EF6 you can implement a pluralization convention, and replace the default English pluralization with your own.

The original implementation uses a convention, which calls a service. I'm not sure whether you can simply register a service for IPluralizationService in the DependencyResolver, but you can definitely write your own convention.

The only warning is that the GitHub code relies on internal methods which you need to copy/substitute, e.g.

var entitySet = model.StoreModel.Container.EntitySets.SingleOrDefault(
                    e => e.ElementType == GetRootType(item));

replacing original

model.StoreModel.GetEntitySet(item);

and the method GetRootType().

devio
  • 36,858
  • 7
  • 80
  • 143