0

If I have n tables in my Db schema and if I want to walk through all tables in my Db and create a .cs file for each table that will contain the below generated code:

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

         get{ return _tableName; }
}

How should my template will be written?

pencilCake
  • 51,323
  • 85
  • 226
  • 363
  • Is everything in the code file fixed, apart from that string you've indicated? If so, you don't need any code-generation. If you do need code generation one way is with [tag:T4] templates. Edit: Sorry, just saw the [tag:mygeneration] tag. – George Duckett Jun 29 '11 at 11:36
  • I simplified the idea actually. Here TableName will be changing from a file to another. And I want to see how you can create a loop to walk-through all the tables in the Db. – pencilCake Jun 29 '11 at 11:38

1 Answers1

0

I haven't worked with MyGeneration before but you can do this easily using CodeGenerator. The template would look something like this:

XSL Template

<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>

Result

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

    //...other tables

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

Edit: You can also have it output one table per file - it sounds like that's what you're after.

Josh M.
  • 26,437
  • 24
  • 119
  • 200