Dan Lugg
Dan Lugg

Reputation: 20612

Entity Framework 6, Database-First & Custom Pluralization

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:

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?

Upvotes: 0

Views: 774

Answers (3)

devio
devio

Reputation: 37215

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().

Upvotes: 0

Dan Lugg
Dan Lugg

Reputation: 20612

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.

Upvotes: 0

johng
johng

Reputation: 181

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.

Upvotes: 1

Related Questions