如何使用Dapper从数据库生成模型?

时间:2012-06-15 18:18:41

标签: visual-studio-2010 dapper micro-orm

我来自PetaPoco营地。 PetaPoco有一个T4模板,可以从数据库中生成模型。 Dapper有类似的东西吗?

我使用NuGet安装了Dapper并添加了SqlHelper.cs,但我没有找到任何从数据库生成模型的内容。

11 个答案:

答案 0 :(得分:42)

我刚刚写了一个sql查询来为自己完成这项工作。并在需要时使用额外类型更新它。只需将表名称替换为@@@@。

为了创建一个临时存储过程来调用很多表。例如。 exec createTablePOCO(@tableName)

SELECT 
'public ' + a1.NewType + ' ' + a1.COLUMN_NAME + ' {get;set;}'
,*
 FROM 
(
/*using top because i'm putting an order by ordinal_position on it. 
putting a top on it is the only way for a subquery to be ordered*/
SELECT TOP 100 PERCENT
COLUMN_NAME,
DATA_TYPE,
IS_NULLABLE,
CASE 
    WHEN DATA_TYPE = 'varchar' THEN 'string'
    WHEN DATA_TYPE = 'datetime' AND IS_NULLABLE = 'NO' THEN 'DateTime'
    WHEN DATA_TYPE = 'datetime' AND IS_NULLABLE = 'YES' THEN 'DateTime?'
    WHEN DATA_TYPE = 'int' AND IS_NULLABLE = 'YES' THEN 'int?'
    WHEN DATA_TYPE = 'int' AND IS_NULLABLE = 'NO' THEN 'int'
    WHEN DATA_TYPE = 'smallint' AND IS_NULLABLE = 'NO' THEN 'Int16'
    WHEN DATA_TYPE = 'smallint' AND IS_NULLABLE = 'YES' THEN 'Int16?'
    WHEN DATA_TYPE = 'decimal' AND IS_NULLABLE = 'NO' THEN 'decimal'
    WHEN DATA_TYPE = 'decimal' AND IS_NULLABLE = 'YES' THEN 'decimal?'
    WHEN DATA_TYPE = 'numeric' AND IS_NULLABLE = 'NO' THEN 'decimal'
    WHEN DATA_TYPE = 'numeric' AND IS_NULLABLE = 'YES' THEN 'decimal?'
    WHEN DATA_TYPE = 'money' AND IS_NULLABLE = 'NO' THEN 'decimal'
    WHEN DATA_TYPE = 'money' AND IS_NULLABLE = 'YES' THEN 'decimal?'
    WHEN DATA_TYPE = 'bigint' AND IS_NULLABLE = 'NO' THEN 'long'
    WHEN DATA_TYPE = 'bigint' AND IS_NULLABLE = 'YES' THEN 'long?'
    WHEN DATA_TYPE = 'tinyint' AND IS_NULLABLE = 'NO' THEN 'byte'
    WHEN DATA_TYPE = 'tinyint' AND IS_NULLABLE = 'YES' THEN 'byte?'
    WHEN DATA_TYPE = 'char' THEN 'string'
    WHEN DATA_TYPE = 'timestamp' THEN 'byte[]'
    WHEN DATA_TYPE = 'varbinary' THEN 'byte[]'
    WHEN DATA_TYPE = 'bit' AND IS_NULLABLE = 'NO' THEN 'bool'
    WHEN DATA_TYPE = 'bit' AND IS_NULLABLE = 'YES' THEN 'bool?'
    WHEN DATA_TYPE = 'xml' THEN 'string'
END AS NewType
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = '@@@@'
ORDER BY ORDINAL_POSITION
) AS a1

答案 1 :(得分:7)

从游标

调用存储过程

如果你结合提到的sp mattritchies(参见上面的答案)并从游标中调用它,你就可以为数据库中的每个表生成poco类

USE YourDataBaseName
GO 
    DECLARE @field1 nvarchar(400)
    DECLARE cur CURSOR LOCAL for

    SELECT TABLE_NAME FROM information_schema.tables
    OPEN cur
    FETCH NEXT FROM cur INTO @field1 --, @field2
    WHILE @@FETCH_STATUS = 0 BEGIN          
        exec Helper_CreatePocoFromTableName @field1 -- , @field2            
        fetch next from cur into @field1 -- , @field2
    END

close cur
deallocate cur

提到的存储过程mattritchies

我从mattritchies回答(见上文)中获取了sql并创建了他提到的存储过程并对其进行了一些修改,以便它也添加了类名。如果将Management Studio置于文本输出模式并删除列名称的输出,则会获得所有类的复制粘贴文本:

CREATE PROCEDURE [dbo].[Helper_CreatePocoFromTableName]    
    @tableName varchar(100)
AS
BEGIN
SET NOCOUNT ON;

-- Subquery to return only the copy paste text
Select PropertyColumn from (
    SELECT 1 as rowNr, 'public class ' + @tableName + ' {' as PropertyColumn
    UNION
    SELECT 2 as rowNr, 'public ' + a1.NewType + ' ' + a1.COLUMN_NAME + ' {get;set;}' as PropertyColumn
    -- ,* comment added so that i get copy pasteable output
     FROM 
    (
        /*using top because i'm putting an order by ordinal_position on it. 
        putting a top on it is the only way for a subquery to be ordered*/
        SELECT TOP 100 PERCENT
        COLUMN_NAME,
        DATA_TYPE,
        IS_NULLABLE,
        CASE 
            WHEN DATA_TYPE = 'varchar' THEN 'string'
            WHEN DATA_TYPE = 'nvarchar' THEN 'string' 
            WHEN DATA_TYPE = 'datetime' AND IS_NULLABLE = 'NO' THEN 'DateTime'
            WHEN DATA_TYPE = 'datetime' AND IS_NULLABLE = 'YES' THEN 'DateTime?'
            WHEN DATA_TYPE = 'smalldatetime' AND IS_NULLABLE = 'NO' THEN 'DateTime'
            WHEN DATA_TYPE = 'datetime2' AND IS_NULLABLE = 'NO' THEN 'DateTime'
            WHEN DATA_TYPE = 'smalldatetime' AND IS_NULLABLE = 'YES' THEN 'DateTime?'
            WHEN DATA_TYPE = 'datetime2' AND IS_NULLABLE = 'YES' THEN 'DateTime?'
            WHEN DATA_TYPE = 'int' AND IS_NULLABLE = 'YES' THEN 'int?'
            WHEN DATA_TYPE = 'int' AND IS_NULLABLE = 'NO' THEN 'int'
            WHEN DATA_TYPE = 'smallint' AND IS_NULLABLE = 'NO' THEN 'Int16'
            WHEN DATA_TYPE = 'smallint' AND IS_NULLABLE = 'YES' THEN 'Int16?'
            WHEN DATA_TYPE = 'decimal' AND IS_NULLABLE = 'NO' THEN 'decimal'
            WHEN DATA_TYPE = 'decimal' AND IS_NULLABLE = 'YES' THEN 'decimal?'
            WHEN DATA_TYPE = 'numeric' AND IS_NULLABLE = 'NO' THEN 'decimal'
            WHEN DATA_TYPE = 'numeric' AND IS_NULLABLE = 'YES' THEN 'decimal?'
            WHEN DATA_TYPE = 'money' AND IS_NULLABLE = 'NO' THEN 'decimal'
            WHEN DATA_TYPE = 'money' AND IS_NULLABLE = 'YES' THEN 'decimal?'
            WHEN DATA_TYPE = 'bigint' AND IS_NULLABLE = 'NO' THEN 'long'
            WHEN DATA_TYPE = 'bigint' AND IS_NULLABLE = 'YES' THEN 'long?'
            WHEN DATA_TYPE = 'tinyint' AND IS_NULLABLE = 'NO' THEN 'byte'
            WHEN DATA_TYPE = 'tinyint' AND IS_NULLABLE = 'YES' THEN 'byte?'
            WHEN DATA_TYPE = 'char' THEN 'string'                       
            WHEN DATA_TYPE = 'timestamp' THEN 'byte[]'
            WHEN DATA_TYPE = 'varbinary' THEN 'byte[]'
            WHEN DATA_TYPE = 'bit' AND IS_NULLABLE = 'NO' THEN 'bool'
            WHEN DATA_TYPE = 'bit' AND IS_NULLABLE = 'YES' THEN 'bool?'
            WHEN DATA_TYPE = 'xml' THEN 'string'
        END AS NewType
        FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE TABLE_NAME = @tableName
        ORDER BY ORDINAL_POSITION
        ) AS a1 
    UNION 
    SELECT 3 as rowNr,  '} // class ' + @tableName
    ) as t Order By rowNr asc
END

P.S。:我会把它作为对他答案的编辑建议,但我的经验是经常编辑建议被拒绝。

更新

用户chris-w-mclean建议我自己未尝试过以下更改(请参阅his suggested-edit):

  • SELECT 1 as rowNr, 'public class '替换为SELECT 1.0 as rowNr, 'public class '
  • SELECT 2 as rowNr, 'public '替换为SELECT 2 + a1.ORDINAL_POSITION/1000 as rowNr, 'public '
  • SELECT TOP 100 PERCENT COLUMN_NAME,替换为SELECT COLUMN_NAME,
  • IS_NULLABLE, CASE此行cast(ORDINAL_POSITION as float) as ORDINAL_POSITION,
  • 之间添加
  • 删除ORDER BY ORDINAL_POSITION
  • SELECT 3 as更改为SELECT 3.0 as

答案 2 :(得分:4)

Dapper本身为连接对象提供了很少的扩展方法(Query,Execute),并且没有“模型生成器”。也许可以使用其他一些框架来生成基于db模式的POCO。

<强>更新

Database tables to C# POCO classes T4 template

<#@ template language="C#" debug="True" #>

<#@ assembly name="System" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>

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

<#@ import namespace="System" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="Microsoft.SqlServer.Management.Common" #>

namespace Namespace
{

<#         
   var databaseName = "testDb";
            var serverConnection = new SqlConnection(
                    @"Data Source=.\SQLEXPRESS; Integrated Security=true; Initial Catalog=" + databaseName);
            var svrConnection = new ServerConnection(serverConnection);

   Server srv = new Server(svrConnection);
            foreach (Table table in srv.Databases[databaseName].Tables)
            {

#>
        class <#= table.Name #>
        {
            <#
                foreach (Column col in table.Columns)
                {
                #>
                    public <#= GetNetDataType(col.DataType.Name) #> <#= col.Name #> { get; set; }
                <#
                }
             #>
        }

<#            }
#>
}



<#+
        public static string GetNetDataType(string sqlDataTypeName)
        {

            switch (sqlDataTypeName.ToLower())
            {
                case "bigint":
                    return "Int64";
                case "binary":
                    return "Byte[]";
                case "bit":
                    return "bool";
                case "char":
                    return "char";
                case "cursor":
                    return string.Empty;
                case "datetime":
                    return "DateTime";
                case "decimal":
                    return "Decimal";
                case "float":
                    return "Double";
                case "int":
                    return "int";
                case "money":
                    return "Decimal";
                case "nchar":
                    return "string";
                case "numeric":
                    return "Decimal";
                case "nvarchar":
                    return "string";
                case "real":
                    return "single";
                case "smallint":
                    return "Int16";
                case "text":
                    return "string";
                case "tinyint":
                    return "Byte";
                case "varbinary":
                    return "Byte[]";
                case "xml":
                    return "string";
                case "varchar":
                    return "string";
                case "smalldatetime":
                    return "DateTime";
                case "image":
                    return "byte[]";

                default:
                    return string.Empty;
            }



        }
#>

答案 3 :(得分:3)

尝试这个版本我优化了一下,这样结果就不需要通过管道输出到Text输出了。相反,PRINT语句允许轻松复制/粘贴输出。我还删除了子查询并添加了nvarchar / ntext类型的声明。

这适用于单个表,但可以将其转换为存储过程以使用上述游标建议之一。

SET NOCOUNT ON
DECLARE @tbl as varchar(255)
SET @tbl = '@@@@'

DECLARE @flds as varchar(8000)
SET @flds=''

SELECT -1 as f0, 'public class ' + @tbl + ' {' as f1 into #tmp

INSERT #tmp
SELECT 
    ORDINAL_POSITION, 
    '    public ' + 
    CASE 
        WHEN DATA_TYPE = 'varchar' THEN 'string'
        WHEN DATA_TYPE = 'nvarchar' THEN 'string'
        WHEN DATA_TYPE = 'text' THEN 'string'
        WHEN DATA_TYPE = 'ntext' THEN 'string'
        WHEN DATA_TYPE = 'char' THEN 'string'
        WHEN DATA_TYPE = 'xml' THEN 'string'
        WHEN DATA_TYPE = 'datetime' AND IS_NULLABLE = 'NO' THEN 'DateTime'
        WHEN DATA_TYPE = 'datetime' AND IS_NULLABLE = 'YES' THEN 'DateTime?'
        WHEN DATA_TYPE = 'int' AND IS_NULLABLE = 'YES' THEN 'int?'
        WHEN DATA_TYPE = 'int' AND IS_NULLABLE = 'NO' THEN 'int'
        WHEN DATA_TYPE = 'smallint' AND IS_NULLABLE = 'NO' THEN 'Int16'
        WHEN DATA_TYPE = 'smallint' AND IS_NULLABLE = 'YES' THEN 'Int16?'
        WHEN DATA_TYPE = 'decimal' AND IS_NULLABLE = 'NO' THEN 'decimal'
        WHEN DATA_TYPE = 'decimal' AND IS_NULLABLE = 'YES' THEN 'decimal?'
        WHEN DATA_TYPE = 'numeric' AND IS_NULLABLE = 'NO' THEN 'decimal'
        WHEN DATA_TYPE = 'numeric' AND IS_NULLABLE = 'YES' THEN 'decimal?'
        WHEN DATA_TYPE = 'money' AND IS_NULLABLE = 'NO' THEN 'decimal'
        WHEN DATA_TYPE = 'money' AND IS_NULLABLE = 'YES' THEN 'decimal?'
        WHEN DATA_TYPE = 'bigint' AND IS_NULLABLE = 'NO' THEN 'long'
        WHEN DATA_TYPE = 'bigint' AND IS_NULLABLE = 'YES' THEN 'long?'
        WHEN DATA_TYPE = 'tinyint' AND IS_NULLABLE = 'NO' THEN 'byte'
        WHEN DATA_TYPE = 'tinyint' AND IS_NULLABLE = 'YES' THEN 'byte?'
        WHEN DATA_TYPE = 'timestamp' THEN 'byte[]'
        WHEN DATA_TYPE = 'varbinary' THEN 'byte[]'
        WHEN DATA_TYPE = 'bit' AND IS_NULLABLE = 'NO' THEN 'bool'
        WHEN DATA_TYPE = 'bit' AND IS_NULLABLE = 'YES' THEN 'bool?'
    END + ' ' + COLUMN_NAME + ' {get;set;}'
FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE TABLE_NAME = @tbl

INSERT #tmp SELECT 999, '}'

SELECT @flds=@flds + f1 +'
' from #tmp order by f0

DROP TABLE #tmp

PRINT @flds

答案 4 :(得分:2)

我知道这是一个老话题,但还有一个简单的选项可以选择。

您可以使用PocoClassGenerator: Mini Dapper's POCO Class Generator (Support Dapper Contrib)

  • 支持当前DataBase所有表和视图生成POCO类代码
  • 支持Dapper.Contrib
  • 支持多种关系型数据库:sqlserver,oracle,mysql,postgresql
  • mini 和更快(仅在 5 秒内生成 100 个表代码)
  • 为每个数据库查询使用适当的方言模式表 SQL

演示

20190430141947-image.png

入门

?首先:将 PocoClassGenerator.cs 代码复制并粘贴到您的项目或 LINQPad。
或从 NuGet

安装
PM> install-package PocoClassGenerator

?第二:使用Connection调用GenerateAllTables,然后打印出来。

using (var connection = Connection)
{
    Console.WriteLine(connection.GenerateAllTables());
}
支持 Dapper Contrib POCO 类
  • 只需使用 GeneratorBehavior.DapperContrib 调用方法
using (var conn = GetConnection())
{
    var result = conn.GenerateAllTables(GeneratorBehavior.DapperContrib);
    Console.WriteLine(result);
}

在线演示:POCO Dapper Contrib Class Generator GenerateAllTables | .NET Fiddle 20190502132948-image.png

生成评论
using (var conn = GetConnection())
{
    var result = conn.GenerateAllTables(GeneratorBehavior.Comment);
    Console.WriteLine(result);
}
生成视图
using (var conn = GetConnection())
{
    var result = conn.GenerateAllTables(GeneratorBehavior.View);
    Console.WriteLine(result);
}
生成视图和评论以及 Dapper.Contrib
using (var conn = GetConnection())
{
    var result = conn.GenerateAllTables(GeneratorBehavior.View | GeneratorBehavior.Comment | GeneratorBehavior.DapperContrib);
    Console.WriteLine(result);
}
通过sql生成一个类
  1. 生成一个类
using (var connection = Connection)
{
    var classCode = connection.GenerateClass("select * from Table");
    Console.WriteLine(classCode);
}
  1. 指定类名
using (var connection = Connection)
{
    var classCode = connection.GenerateClass("with EMP as (select 1 ID,'WeiHan' Name,25 Age) select * from EMP", className: "EMP");
    Console.WriteLine(classCode);
}
数据表Poco类

代码位于 DataTablePocoClassGenerator.cs

var dt = new DataTable();
dt.TableName = "TestTable";
dt.Columns.Add(new DataColumn() { ColumnName = "ID", DataType = typeof(string) });

var result = dt.GenerateClass();
var expect =
@"public class TestTable
{
public string ID { get; set; }
}";
Assert.Equal(expect, result);

答案 5 :(得分:1)

这里是dapper-pocos,我是为Dapper生成POCO而创建的。该解决方案使用SQL Server的“ sp_HELP”和“ sp_describe_first_result_set”。给它一个存储过程的名称,或给它一个select语句,它将生成相关的POCO供Dapper使用。该应用程序只是将存储过程或select语句传递给sp_Help和sp_describe_first_result_set,然后将结果映射到C#数据类型。

答案 6 :(得分:0)

我的方法是:

  1. 使用<dynamic>来获取一些没有类型的行
  2. 将这些行序列化为JSON
  3. 从控制台(或使用调试器)复制JSON字符串
  4. 将其粘贴到JSON到C#模型生成器中(例如https://app.quicktype.io/)。

即:

var persons = connection.Query<dynamic>("SELECT * FROM Persons");
var serializedPerson = JsonConvert.Serialize(persons.First());
Console.WriteLine(serializedPerson);

答案 7 :(得分:0)

这是针对Oracle的。它可能还不完整,但是到目前为止对我来说是有效的。

SELECT 
'public ' || A.NewType || ' ' || REPLACE(INITCAP(REPLACE(A.COLUMN_NAME, '_', ' ')), ' ', '') || ' {get;set;}' GET_SET
, A.*
 FROM 
(
SELECT
COLUMN_NAME,
DATA_TYPE,
NULLABLE,
CASE 
    WHEN DATA_TYPE = 'VARCHAR2' THEN 'string'
    WHEN DATA_TYPE = 'VARCHAR' THEN 'string'
    WHEN DATA_TYPE = 'DATE' AND NULLABLE = 'N' THEN 'DateTime'
    WHEN DATA_TYPE = 'DATE' AND NULLABLE = 'Y' THEN 'DateTime?'
    WHEN DATA_TYPE = 'INT' AND NULLABLE = 'N' THEN 'int?'
    WHEN DATA_TYPE = 'INT' AND NULLABLE = 'Y' THEN 'int'
    WHEN DATA_TYPE = 'DECIMAL' AND NULLABLE = 'N' THEN 'decimal'
    WHEN DATA_TYPE = 'DECIMAL' AND NULLABLE = 'Y' THEN 'decimal?'
    WHEN DATA_TYPE = 'NUMBER' AND NULLABLE = 'N' THEN 'decimal'
    WHEN DATA_TYPE = 'NUMBER' AND NULLABLE = 'Y' THEN 'decimal?'
    WHEN DATA_TYPE = 'NUMBER2' AND NULLABLE = 'N' THEN 'decimal'
    WHEN DATA_TYPE = 'NUMBER2' AND NULLABLE = 'Y' THEN 'decimal?'
    WHEN DATA_TYPE = 'CHAR' THEN 'string'
    WHEN DATA_TYPE = 'CHAR2' THEN 'string'
    WHEN DATA_TYPE = 'timestamp' THEN 'byte[]'
    WHEN DATA_TYPE = 'CLOB' THEN 'byte[]'
    ELSE '??'
END AS NewType
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME = 'FIN_GLOBAL_CR_NUM_A'
ORDER BY COLUMN_ID
) A

答案 8 :(得分:0)

我是一个名为 CodegenCS.POCO 的POCO-Generator模板的作者。

上面的链接包含C#和PowerShell脚本,它们使您可以构建完整的POCO(可在Dapper中使用),其中还包括override bool Equals()override int GetHashCode(),并且(对于喜欢它的人)包括完整的 ActiveRecord CRUD查询(插入/更新)。

从Northwind数据库中检出此example POCO 。如果愿意,可以很容易地使用模板:

generator script非常易于理解和自定义。

答案 9 :(得分:0)

我有完全相同的要求来从数据库生成对象,同时在Dapper中可靠而有效地处理CRUD,并采取了不同的方法来准备Dapper自己的Dapper的替代品。在实体框架模式定义的支持下进行了贡献,以便搭建数据库(模型,关系,键)可以使用Entity Framework工具完成,例如here,示例如下:

network

在脚手架安装后,可以从项目中删除以上依赖项。

当前Dapper.SqlGenerator已成功进行生产。就性能而言,它不会比Dapper产生任何开销,有时会减少通过其他方式生成查询的时间。

请记住,有2个独立的nuget包-Dapper.SqlGenerator用于从EF(核心)支架模型生成纯SQL代码,Dapper.SqlGenerator.Async用于使用Dapper对数据库运行CRUD查询。

TLDR;您可以使用实体框架(核心)从数据库中构建模型,并使用Dapper.SqlGenerator在生成的对象上生成CRUD查询。

答案 10 :(得分:0)

我见过人们使用混合项目,使用 EF 搭建数据库的地方,但我不得不精简它的输出。对于推荐的工具,我确信它们很好,但我避免安装特殊软件,直到我尝试编写自己的解决方案。

也就是说,这是一个可能有用的小型 CLI 程序(满足我的需要)。免责声明,我不是一个经验丰富的 C# 程序员,所以请原谅任何可能不正常的事情。

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using Dapper;

namespace Pocos
{
    public class TAB {
        public string TABLE_NAME { get; set; }
    }
    public class COL {
        public string COLUMN_NAME { get; set; }
        public int? ORIDINAL_POSITIONS { set; get; }
        public string DATA_TYPE { get; set; }
        public string CHARACTER_MAXIMUM_LENGTH { get; set; }
        public string NUMERIC_PRECISION { get; set; }
        public string NUMERIC_SCALE { get; set; }
    }
    class Program {
        static void Main(string[] args) {
            string sConnect = "Server=LT6-MARKL;Database=PKDEM815;UID=PKDEM815;Password=PKDEM815";
            IEnumerable tables;
            IEnumerable columns;
            List lines;
            using ( var conn = new SqlConnection(sConnect))
                tables = conn.Query("SELECT * FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME");
            // Roll through each table of the database and generate an .cs file, as a POCO
            foreach (TAB t in tables.OrderBy(t => t.TABLE_NAME)) {
                lines = new List();
                lines.Add("using System;");
                lines.Add("using System.Collections.Generic;");
                lines.Add("using System.Configuration;");
                lines.Add("using System.Data.SqlClient;");
                lines.Add("using Dapper;"); 
                lines.Add("using Dapper.Contrib.Extensions;");
                lines.Add("");
                lines.Add("namespace PKDataLayer.Models  {");
                lines.Add("");
                lines.Add("\t[Table(\"" + t.TABLE_NAME + "\")]");
                lines.Add("\tpublic class " + t.TABLE_NAME + " {");
                lines.Add("");
                using (var conn2 = new SqlConnection(sConnect)) {
                    columns = conn2.Query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '"+ t.TABLE_NAME +"' ORDER BY ORDINAL_POSITION");
                    foreach( COL c in columns) {
                        if (t.TABLE_NAME + "_KEY" == c.COLUMN_NAME || t.TABLE_NAME + "_SEQNUM" == c.COLUMN_NAME)
                            lines.Add("\t\t[Key]");
                        // SELECT DISTINCT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME IN ( SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES )
                        if (c.DATA_TYPE == "char" || c.DATA_TYPE == "varchar")
                            lines.Add("\t\tpublic string " + c.COLUMN_NAME + " { get; set; }");
                        if (c.DATA_TYPE == "int")
                            lines.Add("\t\tpublic int " + c.COLUMN_NAME + " { get; set; }");
                        if (c.DATA_TYPE == "datetime")
                            lines.Add("\t\tpublic DateTime? " + c.COLUMN_NAME + " { get; set; }");
                        if (c.DATA_TYPE == "decimal" || c.DATA_TYPE == "numeric")
                            lines.Add("\t\tpublic decimal? " + c.COLUMN_NAME + " { get; set; }");
                    }
                }
                lines.Add("\t}");
                lines.Add("}");
                Console.WriteLine("Creating POCO for " + t.TABLE_NAME);
                using (TextWriter tw = new StreamWriter( t.TABLE_NAME + ".cs" ))
                    foreach (String s in lines)
                        tw.WriteLine(s);
            }
        }
    }
}