简体   繁体   English

实体框架核心 - 自定义脚手架

[英]Entity Framework Core - Customise Scaffolding

In Entity Framework 6 we can add the T4 templates the scaffolding uses by running 在Entity Framework 6中,我们可以通过运行添加脚手架使用的T4模板

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. 但是在Entity Framework Core中,脚手架系统似乎没有使用T4模板,看起来脚手架也不能定制。 It seems to be all in c# classes eg. 它似乎都在c#类中,例如。

https://github.com/aspnet/EntityFramework/blob/a508f37cf5a0246e9b92d05429153c3d817ad5ec/src/Microsoft.EntityFrameworkCore.Tools.Core/Scaffolding/Internal/EntityTypeWriter.cs 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
}

Update: See Yehuda Goldenberg 's answer for another way to do this in EF Core 1.0.2+. 更新:请参阅Yehuda Goldenberg的回答,了解另一种在EF Core 1.0.2+中执行此操作的方法。

In .Net Core 1.1, the way to override design-time services is to implement the IDesignTimeServices interface in the startup assembly. 在.Net Core 1.1中,覆盖设计时服务的方法是在启动程序集中实现IDesignTimeServices接口。 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 请参阅https://github.com/aspnet/EntityFramework/issues/5617

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM