不同数据上下文的把手模板

Handlebars templates for different datacontexts

我在同一个项目中有两个看起来不同的数据库上下文。 一个继承自不同的基 class 并具有不同的构造函数。

public partial class DbFirstGraphQLDataContext : DbContext
    {
        public DbFirstGraphQLDataContext(DbContextOptions options) : base(options)
        {
        }

public partial class DbFirstOtherDataContext : DbContextCustomBase
    {
        public DbFirstGraphQLDataContext(DbContextOptions options, IServiceCollection serviceCollection) : base(options, serviceCollection)
        {
        }

我可以先使用典型命令搭建其中一个: dotnet ef dbcontext scaffold -c DbFirstGraphQLDataContext ...

我有基本的脚手架设计时间服务:

    public class ScaffoldingDesignTimeServices : IDesignTimeServices
    {
        public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
        {
            serviceCollection.AddHandlebarsScaffolding(opts=> opts.TemplateData);
        }
     }

.hbs文件,我已经粘贴了其中的一部分。如您所见,.hbs 文件用于 DbFirstGraphQLDataContext

{{> dbimports}}
using DA.SomeInternalRepo;

namespace {{namespace}}
{
    //This file is autogenerated using EF database first. Do not modify it. Customisations can be made using the .hbs template files
    public partial class {{class}} : DbContextCustomBase
    {

我应该如何编写模板、C# 代码或脚本参数,以便根据呈现的上下文呈现不同的构造函数或基础 class

在 AddHandlebarsScaffolding 调用中,将您的基础 class 添加到模板数据中:

options.TemplateData = new Dictionary<string, object>
{    
    { "base-class", "MyBaseClass" }
};

然后在您的“Class.hbs”模板中添加此引用

public partial class {{class}} : {{base-class}}

然后构建...它将使用您定义的基础class。根据您需要生成的上下文进行设置...