SHARE:

Entity Framework Core 2 – Pluralization and Singularization

 

Entity Framework Core 2 was released on August 14th. It brought new features.

On this article I will explain one of them : Pluralization and Singularization

There is a new IPluralizer interface. It can be used to pluralize table names when EF is generating the database (dotnet ef database update) or entities when generating classes from it (Scaffold-DbContext). The way to use it is somewhat tricky, as we need to have a class implementing IDesignTimeServices, and this class will be discovered automatically by these tools.

In this example I provide a custom implementation with the Nuget package Inflector

Example:

public class CustomPluralizer : IPluralizer
{
   public string Pluralize(string name)
   {
      return Inflector.Inflector.Pluralize(name) ?? name;
   }
   public string Singularize(string name)
   {
      return Inflector.Inflector.Singularize(name) ?? name;
   }
}
public class CustomDesignTimeServices : IDesignTimeServices
{
   public void ConfigureDesignTimeServices(IServiceCollection services)
   {
     services.AddSingleton<IPluralizer, CustomPluralizer>();
   }
}

That’s it 🙂

Written by

anthonygiretti

Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.
%d bloggers like this: