Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

c# - Entity Framework Core - Customise Scaffolding

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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+.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...