如何使用Dapper從數據庫生成模型?

dapper micro-orm visual-studio-2010

我來自PetaPoco營地。 PetaPoco有一個T4模板,可以從數據庫中生成模型。 Dapper有類似的東西嗎?

我使用NuGet安裝了Dapper並添加了SqlHelper.cs,但是我沒有找到任何從數據庫生成模型的東西。

一般承認的答案

Dapper本身為連接對象提供了很少的擴展方法(Query,Execute),並且沒有“模型生成器”。也許某些其他框架可用於基於db模式生成POCO。

更新:

數據庫表到C#POCO類T4模板

<#@ 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;
            }



        }
#>

熱門答案

我剛剛寫了一個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


許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因