【问题标题】:How can I achieve this simple output in MyGeneration?如何在 MyGeneration 中实现这个简单的输出?
【发布时间】:2011-09-25 01:55:45
【问题描述】:

如果我的 Db 架构中有 n 个表,并且我想遍历我的 Db 中的所有表并创建一个 .cs 每个表的文件将包含以下生成的代码:

public class TableName
{
  private _tableName = "<%TableName%>" //This string will be generated by MyGeneration 
                                  // per each table
  public string TableName {

         get{ return _tableName; }
}

我的模板应该怎么写?

【问题讨论】:

  • 除了您指出的那个字符串之外,代码文件中的所有内容是否都已修复?如果是这样,您不需要任何代码生成。如果您确实需要代码生成,一种方法是使用t4 模板。编辑:抱歉,刚刚看到mygeneration 标签。
  • 我实际上简化了这个想法。此处 TableName 将从一个文件更改为另一个文件。我想看看如何创建一个循环来遍历 Db 中的所有表。

标签: c# .net code-generation mygeneration


【解决方案1】:

我之前没有使用过 MyGeneration,但您可以使用 CodeGenerator 轻松完成此操作。模板看起来像这样:

XSL 模板

<xsl:stylesheet version="1.0" xmlns:P="http://Schemas.QuantumConceptsCorp.com/CodeGenerator/Project.xsd" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
    <xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>

    <xsl:template match="P:Project">
        <xsl:text>
namespace  </xsl:text>
        <xsl:value-of select="@RootNamespace"/>
<xsl:text>.DataObjects
{</xsl:text>
        <xsl:for-each select="P:TableMappings/P:TableMapping[@Exclude='false']">
            <xsl:text>
    public partial class </xsl:text>
            <xsl:value-of select="@ClassName"/>
            <xsl:text>  
    {
        private string TableName  { get { return "</xsl:text>
            <xsl:value-of select="@ClassName"/>
            <xsl:text>"; } }
    }
    </xsl:text>
    </xsl:template>
</xsl:stylesheet>

结果

namespace [Your.Namespace]
{
    public class [TableName1]
    {
        public string TableName { get { return "[TableName1]"; } }
    }

    //...other tables

    public class [TableNameN]
    {
        public string TableName { get { return "[TableNameN]"; } }
    }
}

编辑:您还可以让它为每个文件输出一个表 - 听起来这就是您所追求的。

【讨论】:

    猜你喜欢
    • 2019-09-02
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2020-06-07
    • 2012-03-18
    • 1970-01-01
    相关资源
    最近更新 更多