首页 > 代码库 > 使用T4模板调用Sqlserver链接生成自己的模板

使用T4模板调用Sqlserver链接生成自己的模板

<#@ template debug="false" hostspecific="false" language="C#" #><#@ assembly name="Microsoft.CSharp" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ assembly name="System.Data" #><#@ import namespace="System.Collections.Generic" #><#@ import namespace="System.Data.SqlClient" #><#@ import namespace="System.Dynamic" #><#@ output extension=".cs" #><#     var namespaceName="Reap.DAL.Repositories";    var connectionString = @"data source=xxx;Initial Catalog=xxx;User ID=sa;Password=xx;";        using (var db = new SqlConnection (connectionString))     using (var cmd = db.CreateCommand())     {        db.Open();        var tables= ReadRows (cmd, "SELECT * FROM sys.tables WHERE NAME<>‘__MigrationHistory‘").ToArray();#>using Reap.Models.Models;using Reap.IDAL.Repositories;using Reap.DAL.UnitOfWork;namespace <#=namespaceName#>{  <#        foreach (var table in tables)        {           #>     public class I<#=table.name#>Repository:EFRepositoryBase<<#=table.name#>>,I<#=table.name#>Repository{        }  <#     }     }  #>  }  <#+   static IEnumerable<dynamic> ReadRows (SqlCommand command, string sql)     {        command.CommandText = sql ?? "";        using (var reader = command.ExecuteReader())        {           while (reader.Read())           {              var dyn = new ExpandoObject ();              IDictionary<string, object> dic = dyn;              for (var iter = 0; iter < reader.FieldCount; ++iter)              {                    dic[reader.GetName(iter) ?? ""] = reader.GetValue(iter);              }              yield return dyn;           }        }     }  #>