繁体   English   中英

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

[英]Handlebars templates for different datacontexts

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

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 ...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# 代码或脚本参数,以便它根据正在呈现的上下文呈现不同的构造函数或基类

在 AddHandlebarsScaffolding 调用中,将您的基类添加到 TemplateData 中:

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

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

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

并构建...它将使用您定义的基类。 根据您需要生成的上下文设置它...

暂无
暂无

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

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