In Entity Framework 6 we can add the T4 templates the scaffolding uses by running
Install-Package EntityFramework.CodeTemplates.CSharp
But in Entity Framework Core the scaffolding system does not appear to use T4 templates, nor does it seem like the scaffolding can be customised. It seems to be all in c# classes eg.
https://github.com/aspnet/EntityFramework/blob/a508f37cf5a0246e9b92d05429153c3d817ad5ec/src/Microsoft.EntityFrameworkCore.Tools.Core/Scaffolding/Internal/EntityTypeWriter.cs
Is there any way to customise the output from the scaffold?
Scaffolding a database produces an Entity Framework model from an existing database. The resulting entities are created and mapped to the tables in the specified database. For an overview of the requirements to use EF Core with MySQL, see Table 7.2, “Connector/NET Versions and Entity Framework Core Support”).
The above Scaffold-DbContext command creates entity classes for each table in the SchoolDB database and context class (by deriving DbContext ) with Fluent API configurations for all the entities in the Models folder. The following is the generated Student entity class for the Student table.
There is a special, yet-to-be-documented hook to override design-time services:
class Startup
{
public static void ConfigureDesignTimeServices(IServiceCollection services)
=> services.AddSingleton<EntityTypeWriter, MyEntityTypeWriter>();
}
Then implement your custom generator.
class MyEntityTypeWriter : EntityTypeWriter
{
public EntityTypeWriter(CSharpUtilities cSharpUtilities)
: base(cSharpUtilities)
{
}
// TODO: Override with custom implementation
}
Update: See Yehuda Goldenberg's answer for another way to do this in EF Core 1.0.2+.
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