Entity Framework Core - Customise Scaffolding

c# entity-framework-core

Question

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?

1
11
6/7/2016 12:25:55 PM

Accepted Answer

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

7
5/23/2017 11:45:41 AM

Popular Answer

In .Net Core 1.1, the way to override design-time services is to implement the IDesignTimeServices interface in the startup assembly. For example:

public class MyDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
    {
        serviceCollection.AddSingleton<EntityTypeWriter, MyEntityTypeWriter>();
        serviceCollection.AddSingleton<DbContextWriter, MybContextWriter>();

    }
}

See https://github.com/aspnet/EntityFramework/issues/5617



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow