Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6803491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:21:04+00:00 2026-05-26T19:21:04+00:00

I am trying to generate the below code via T4. using System; using MyDAL;

  • 0

I am trying to generate the below code via T4.

using System;
using MyDAL;

namspace Test
{
// Select...
   public partial class MyCustomer
   {
      public int id { get ;set;}
      public string name { get ;set;}
      public string surname { get ;set;}

      public MyCustomer()
      {

      }

      public List<Customer> GetById(int id)
      {
         // do something...
      }
   }
        // Delete,Update,Save
   public partial class MyCustomer
   {
       public bool Save(new MyCustomer)
       {
         // do something...
       }

       public bool delete(int id)
       {
         // do something...
       }

       public bool Update(int id)
       {
         // do something...
       }
   }
}

The T4 template I have so far:


<#@ template language="C#"#>
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.Sfc" #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>



<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>

namespace DbClasses
{
    <#

    Server srv = new Server();
    Database db = new Database(srv,"NarDb");
    db.Refresh();
    foreach (Table tablo in db.Tables)
        {

             WriteLine("public class " +tablo.Name+"{");
            foreach (Column column in tablo.Columns)
                {
                    if(column.DataType.Name=="nvarchar")
                             WriteLine("public "+"string " +""+column.Name+";");
                    else
                         WriteLine("public "+"int " +""+column.Name+";");
                }
                WriteLine("}");



        }
   #>
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:21:04+00:00Added an answer on May 26, 2026 at 7:21 pm

    I’m not exactly sure what is the problem you are facing: but there is nothing wrong with your T4 code. The signature of the Save method in the second class has new and that’s an error:

    public bool Save(new MyCustomer)
    

    should be

    public bool Save(MyCustomer customer)
    

    Also, GetById should return one element not a list.

    I tried out the code you wrote without connecting to a database by writing dummy classes for Table, Column, etc and there is nothing wrong with the T4 code you wrote, except that, as per your requirements, the code your wrote doesn’t generate partial classes, not does it create properties. It just creates a single non-partial class for each table with public fields (not properties). If you want to generate partial classes and properties (and methods) you’re in the right direction (I added tabs and newlines to make it look pretty in the generated file, you can just press Ctrl+K, Ctrl+D there to reformat it though:

    <#
    
    foreach (Table tablo in tables)
    {
    //start class
    WriteLine("public partial class My" +tablo.Name+"\n{");
    foreach (Column column in tablo.Columns)
    {
    if(column.DataType.Name=="nvarchar")
    WriteLine("\tpublic string " + column.Name+"{get; set;}");
    else
    WriteLine("\tpublic int " + column.Name+"{get; set;}");
    }
    //output the contructor
    WriteLine("\tpublic My" + tablo.Name + "()\n\t{\n\t\t//constructor\n\t}" );
    WriteLine("\tpublic List<" + tablo.Name + "> GetById(int id)\n\t{\n\t\t// do something\n\t}");
    //end class
    WriteLine("}");
    
    //output comment and start class
    WriteLine("//Delete,Update,Save");
    WriteLine("public partial class My" +tablo.Name+"\n{");
    WriteLine("\tpublic bool Save(My" + tablo.Name + " " + tablo.Name + ")\n\t{\n\t\t// do something\n\t}");
    WriteLine("\tpublic bool Delete(int id)\n\t{\n\t\t// do something\n\t}");
    WriteLine("\tpublic bool Update(int id)\n\t{\n\t\t// do something\n\t}");
    WriteLine("}");
    
    }
    #>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to generate code coverage reports with EMMA using tests of which some
I am trying to generate an RSS feed on my site using the code
I am trying with below code to generate 10 digits unique random number. As
I have the following code below to generate an OpenSSL RSA public and private
I'm trying to generate some code at runtime where I put in some boiler-plate
I'm a beginner and trying to generate as much of my own code as
Trying to generate an RSA Public Key given an APIs modulus and exponent. I'm
i am trying to generate schema using jaxb from my exisitng POJO classes and
The code below is the code i am using. It works fine in thunderbird
I'm trying to calculate SHA1 hash of a unicode string using T-SQL. The below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.