With the release of Entity Framework Core 2.1 we finally have a version of EF Core that is ready for prime time. EF Core is a complete re-write of its predecessor Entity Framework 6, which has been married to the full Windows .NET Framework since it was released in 2008 as part of .NET 3 SP1. EF Core, on the other hand, was designed using modern programming concepts such as greater modularity via dependency injection and can run on non-Windows platforms that provide more options for cloud-based deployments that are serverless or use containers.
But writing a new data access stack from scratch required numerous tradeoffs. On the plus side, the EF Core team added long awaited but never realized features, such as mixed client/server query evaluation and composable queries that include raw SQL. On the downside, they had to cut features considered essential to some teams, such as table-per-type inheritance modeling and support for mapping stored procedures to CUD operations. On balance, however, as of version 2.1 EF Core has been able to achieve much better parity with EF 6 by including previous show-stoppers such as GroupBy query translation and support for System.Transactions. (See this article for a feature-by-feature comparison between EF Core and EF 6.)
While the EF Core runtime has matured to the point where it can be considered a viable option for enterprise applications, the tooling has lagged behind. For example, there is still no Visual Studio wizard for reverse engineering an existing database to model and context classes. For that you’ll need to resort to the command line.
Note: Erik Ejlskov Jensen has authored the EF Core Power Tools, which provide a UI for reverse engineering classes from an existing database and use my Handlebars plugin under the covers. They also allow you to perform migrations and visualize your DbContext in various ways.
While that doesn’t present too much difficulty, there has not been a way to customize classes generated by the EF Core tooling.
That is, until now.
I have authored a plug-in (EntityFrameworkCore.Scaffolding.Handlebars) that allows you to use Handlebars templates to customize classes that are reverse engineered from an existing database using the
dotnet ef dbcontext scaffold command. To use the plugin simply add my extension NuGet package to your EF Core Class Library project, then add a class that implements
IDesignTimeServices by calling the
AddHandlebarsScaffolding extension method that hangs off
Next, open a command prompt at the project level and use the .NET Core CLI to reverse engineer a context and models from an existing database. For example, if you have downloaded scripts to create the NorthwindSlim sample database for SQL Server LocalDb, you can run the following command:
The first time you run the command you’ll see a CodeTemplates folder magically appear in your project.
There you’ll find Handlebars templates for context and entity classes which you can customize to your heart’s content.
Notice there are also partial templates that you can also customize. (One reason you might want to customize generated entity classes would be to implement an interface or extend a base class.) The next time you run the scaffolding command, you’ll see your changes reflected in the generated classes.
Lastly, my plug-in allows you to register Handlebars helpers for further customizing output based on runtime conditions. Simple pass one or more named tuples to the
AddHandlebarsScaffolding extension method.
Then insert the helper into your Handlebars template as in the following example.
This will result in the helper rendering your desired content, as shown in the following example.
For further information and detailed instructions please visit the ReadMe on the project repo. Enjoy!