Entity Framework Core - 自定义脚手架 [英] Entity Framework Core - Customise Scaffolding

查看:26
本文介绍了Entity Framework Core - 自定义脚手架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实体框架 6 中,我们可以通过运行添加脚手架使用的 T4 模板

In Entity Framework 6 we can add the T4 templates the scaffolding uses by running

Install-Package EntityFramework.CodeTemplates.CSharp

但在 Entity Framework Core 中,脚手架系统似乎不使用 T4 模板,脚手架似乎也不能自定义.它似乎都在 c# 类中,例如.

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?

推荐答案

有一个特殊的、尚未记录的钩子来覆盖设计时服务:

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
}

更新:请参阅 Yehuda Goldenberg 的回答,了解另一种方法在 EF Core 1.0.2+ 中.

Update: See Yehuda Goldenberg's answer for another way to do this in EF Core 1.0.2+.

这篇关于Entity Framework Core - 自定义脚手架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆