This is one of the main use cases for CodeSmith.
I am writing this TEMPLATE in VB, but it can be used to generate any sort of file. Codesmith can use C#, feel free to convert this to C#, fairly straight forward exercise
The first part defines some assemblies that must be referenced.
The Imports then allow the template to reference those assemblies
The Property 'SourceTable' is where the magic happens. When you compile the table the first time, hit F4 and the template properties screen will appear.
Click the elipses '...' to the right of the SourceTable line and point to your DB and table.
You will need to define a new DataSource , this requires a connection string to your database.
An example is
Data Source=DEVMACHINE;Initial Catalog=MyDB;Persist Security Info=True;User ID=myUserId;Password=myPassword
This will get you started. In the routine DeclarationFromColumn, you will want to declare a c# type from the SQL server type.
What has worked for me is to take a complete working class that you already have, paste it into the template, and then start looking for places where you run through all the fields.
In each of those instances, write some template code to generate that list.
All of the functions need to go in the <Script></Script>
tag.
<%@ Template Language="VB"%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table to get the data from." %>
<% dim i as integer %>
public class RespObj
{
<% for i = 0 to SourceTable.Columns.Count - 1 step 1 %>
<%= DeclarationFromColumn(SourceTable.Columns(i)) %>
<% next %>
}
<script runat="template">
function DeclarationFromColumn(clm as ColumnSchema) as string
return "public " & clm.NativeType & " " & clm.Name & " {get; set;}"
end function
</Script>