【问题标题】:Handlebars templates for different datacontexts用于不同数据上下文的车把模板
【发布时间】:2021-09-29 16:08:47
【问题描述】:

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

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

【问题讨论】:

    标签: c# entity-framework-core handlebars.net


    【解决方案1】:

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

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

    然后在你的“Class.hbs”模板中,添加这个引用

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

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

    【讨论】:

      猜你喜欢
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      相关资源
      最近更新 更多