recently I've created an ASP.NET Core (Multiplatform) project and a ClassLibrary for Managing SQL Server Database models by using VS2017.
Actually we have a Database in production server and I need to generate classes by using dotnet ef dbcontext scaffold [arguments] [options] ... command line, however I need to singularize class name while creating DbContext class.
Please, need help! thanks
I've found the solution!
The best solution is to implement the IDesignTimeServices interface and use the inflector tool as explained in https://romiller.com/2017/02/10/ef-core-1-1-pluralization-in-reverse-engineer/.
To put in action such solution, DesignTimeService
, DbContextWriter
, ModelConfiguration
, RelationalScaffoldingModelFactory
custom classes and Inflector
class must reside in startup project where Startup
Class is located. Execute following commands by using package manager console:
Install-Package Microsoft.EntityFrameworkCore.Design
and
Install-Package Microsoft.EntityFrameworkCore.Tools
During Scaffold process, Entity Framework Design searches for any class that implements IDesignTimeServices
into Startup
project and its code is executed.
Execute the following command replacing values as needed by using package manager console keeping in mind that if it is required to store models classes in a Database First process into other project (f.e. Class Library), select it previously in Visual Studio Package Manager Console:
Scaffold-DbContext -provider Microsoft.EntityFrameworkCore.SqlServer -connection "<connection string here...>" -Context MyDbContext -OutputDir "folder path" -StartupProject MyMainProject -Force
That's all, singularization and any customization needed works perfect!
Thanks for help provided in the comments!
UPDATE:
Recent Entity Framework Core 2.0 version has improved the Singularization/Pluralization process avoiding the requirement of DbContextWriter, ModelConfiguration and RelationalScaffoldingModelFactory implementation, making this process simpler through a new interface named IPluralizer which you use in a class that implements the methods Pluralize and Singularize.
Same packages installation of Microsoft.EntityFrameworkCore.Design, Microsoft.EntityFrameworkCore.Tools are required, also Microsoft.EntityFrameworkCore.SqlServer if you want this DataBase in your solution, but now the class that implements IDesignTimeServices only requires the class implementing IPluralizer in a simpley way as:
public class CustomDesignTimeService : IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
=> serviceCollection.AddSingleton<IPluralizer, CustomPluralizer>();
}
Finally, CustomPluralizer just uses Inflector tool class methods:
public class CustomPluralizer : IPluralizer
{
public string Pluralize(string identifier)
{
return Inflector.Pluralize(identifier) ?? identifier;
}
public string Singularize(string identifier)
{
return Inflector.Singularize(identifier) ?? identifier;
}
}
Just implement your own Pluralize/Singularize code if you don't want to use Inflector class.
That's all what I had to do in my new ASP.NET Core 2.0 project. I hope this help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With