Singularize DbSet properties in DataContext

Posts   
 
    
Posts: 2
Joined: 04-Jan-2019
# Posted on: 04-Jan-2019 10:43:35   

Hi all,

I'm using LLBLGen Pro V5.5 and after generating my code i see that the names of my DbSet properties inside the DataContext are pluralized. Because the database i use is in Dutch the english pluralization is worthless. Is there a setting which i can edit that makes sure that the name of the DbSet is the same name as the Entity So for example.

Generated code is for EFCore 2.0

Entity Customer (in dutch Klant) will become


DbSet<Customer> Customers {get; set;}
//In dutch
DbSet<Klant> Klants {get; set;} // This should be Klanten instead of Klants

the preferred solution i'm looking for is something like this:


DbSet<Customer> Customer {get; set;} //So singularized no S on the end
//In dutch
DbSet<Klant> Klant {get; set;}

I hope someone can tell me how to fix this.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39747
Joined: 17-Aug-2003
# Posted on: 04-Jan-2019 11:07:48   

You can disable the pluralization plugin. Go to (in the designer) Tools -> Bind Designer events to plugins, then select with 'Designer event' 'NameSingularToPluralConversion' and uncheck the 'IsEnabled' checkbox.

This also controls the pluralization of names on e.g. navigators etc. The downside of this is that the plugin is disabled for all the projects you load, so if you work on multiple projects this might be less ideal.

You can also adjust the pluralization plugin code, if you want to. The plugin sourcecode comes with the sourcecode archive available under My account -> v5.5 -> Extras section on the website. In the plugins folder, you'll find the Inflector.cs file which is the root of this plugin, it's a bunch of simple regexps which you can e.g. alter to make it work for Dutch (but as Dutch' pluralization rules aren't that simple, this likely is a daunting task).

A 3rd way to solve this is by using a custom Context template for code generation. after you've loaded your project in the designer, click the Template bindings viewer icon in the toolbar (tools -> template bindings viewer). Then double click the SD_EF_Core_Context binding. At the bottom of the template, you'll see:

        public DbSet<<%=entity.Name%>> <%=GeneratorUtils.PluralizeString(entity.Name)%> { get; set; } 

Simply remove the call to GeneratorUtils.PluralizeString and you'll get singular names. It's best if you create a copy of the template and bind it to the same template ID (SD_EF_Core_Context) using a new templatebindings file https://www.llblgen.com/Documentation/5.5/Designer/How%20To/TemplatesAdd.htm and give it a higher precedence number than the vanilla one (so it's picked over the vanilla one, it then overrules the templateID binding set by our templatebinding files).

Frans Bouma | Lead developer LLBLGen Pro
Posts: 2
Joined: 04-Jan-2019
# Posted on: 04-Jan-2019 15:10:54   

Hi Frans,

Thank you so much for the quick response. It's to bad that you can't enable / disable plugins on project basis, especially when you develop for multiple clients with different needs. Is this something that will be possible in a future release? I don't want to mess with the templates.

I took a look at the sourcecode for the plugin and maybe i will try and add the dutch pluralization rules to my own plugin. It looks very easy!

Thank you so much!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39747
Joined: 17-Aug-2003
# Posted on: 07-Jan-2019 09:55:17   

You can't switch / enable then on a per-project basis, but that's indeed something we should add. We'll look into adding that in a future version. The inflector's rules are evaluated from back to front, so a rule added last will overrule any rules defined before it. For Dutch a rule to pluralize to 'n' or 'en' (which is the majority of the names) then should be added to the bottom. Indeed, you can't keep it enabled for other projects if those use English, which is a limitation.

Frans Bouma | Lead developer LLBLGen Pro