public void SQLServerEntitiesCreate() { using (var db = new DapperExContext()) { string sql = string.Format(@"SELECT A.TABLE_NAME AS TableName, H.TableDec AS TableDec, A.COLUMN_NAME AS ColumnName, H.ColumnDec AS ColumnDec, A.COLUMN_DEFAULT AS DefaultValue, CASE WHEN A.IS_NULLABLE = 'YES' THEN 'true' WHEN A.IS_NULLABLE = 'NO' THEN 'false' END AS IsNullable, A.DATA_TYPE AS ColumnType, A.CHARACTER_MAXIMUM_LENGTH AS Length, CASE WHEN B.COLUMN_NAME IS NULL THEN 'false' WHEN B.COLUMN_NAME IS NOT NULL THEN 'true' END AS IsPrimaryKey FROM INFORMATION_SCHEMA.COLUMNS A LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE B ON B.COLUMN_NAME = A.COLUMN_NAME AND B.TABLE_NAME = A.TABLE_NAME LEFT JOIN ( SELECT D.name AS TableName, E.name AS ColumnName, F.VALUE AS ColumnDec, G.VALUE AS TableDec FROM sys.tables D INNER JOIN sys.columns E ON E.object_id = D.object_id LEFT JOIN sys.extended_properties F ON F.major_id = E.object_id AND F.minor_id = E.column_id LEFT JOIN sys.extended_properties G ON G.major_id = E.object_id AND G.minor_id = '0' ) H ON H.TableName = A.TABLE_NAME AND H.ColumnName = A.COLUMN_NAME WHERE A.TABLE_NAME = 'sys_server_info_mst' ORDER BY A.TABLE_NAME ASC" ); var result = db.Query <TableSchema>(sql).ToList(); var resultDictionary = result.GroupBy(x => x.TableName).ToDictionary(k => k.Key, v => v.ToList()); CultureInfo cultureInfo = new CultureInfo("en", false); string content = string.Empty; foreach (KeyValuePair <String, List <TableSchema> > item in resultDictionary) { // 表名去下划线 & 驼峰 string[] tableNameArray = item.Key.ToLower().Split('_'); string nTableName = string.Empty; foreach (string n in tableNameArray) { nTableName += cultureInfo.TextInfo.ToTitleCase(n); } Directory.CreateDirectory("D:\\DapperEntities\\"); using (FileStream fs = new FileStream("D:\\DapperEntities\\" + nTableName + ".cs", FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) { content = string.Format( @"/* 由DapperEntitiesCreate生成 */ using System; using WCFServer.Data.DapperEx.Attributies; namespace WCFServer.Data.DapperEx.Entities {{ /// <summary> /// {0} 【{1}】 /// </summary> [Table(""{2}"")] public class {3} {{", nTableName, item.Value.FirstOrDefault().TableDec, item.Key, nTableName); sw.WriteLine(content); foreach (TableSchema colItem in item.Value) { string colType = string.Empty; if (colItem.ColumnType.ToLower() == "nvarchar" || colItem.ColumnType.ToLower() == "nchar" || colItem.ColumnType.ToLower() == "varchar" || colItem.ColumnType.ToLower() == "text") { colType = "string"; } if (colItem.ColumnType.ToLower() == "int" || colItem.ColumnType.ToLower() == "smallint" || colItem.ColumnType.ToLower() == "tinyint") { colType = "int"; } if (colItem.ColumnType.ToLower() == "decimal" || colItem.ColumnType.ToLower() == "float") { colType = "double"; } if (colItem.ColumnType.ToLower() == "datetime" || colItem.ColumnType.ToLower() == "date") { colType = "DateTime"; } string[] colNameArray = colItem.ColumnName.ToLower().Split('_'); string nColName = string.Empty; foreach (string n in colNameArray) { nColName += cultureInfo.TextInfo.ToTitleCase(n); } content = string.Format( @" /// <summary> /// {0} 【{1}】 /// </summary>", nColName, colItem.ColumnDec); sw.WriteLine(content); content = string.Format( @" [{0}Column(""{1}"")] public {2} {3} {{ get; set; }}", colItem.IsPrimaryKey ? "Key, " : "", colItem.ColumnName, colType, nColName); sw.WriteLine(content); } sw.WriteLine( @" } }"); } } } } }
/// <summary> /// 服务接口提供器 /// </summary> /// <returns></returns> public List <ServiceInfo> ServiceProvider() { List <ServiceInfo> serviceInfoList = new List <ServiceInfo>(); #region 0.默认数据 string ip = "localhost"; int port = 1028; string endpoint = "json"; #endregion #region 1.获取所有的服务接口及其实现类 Dictionary <Type, Type> pairs = new Dictionary <Type, Type>(); var types = TypeHelper.GetIntfChildrens(typeof(IBaseContract)); bool isPair = true; Type type1 = null, type2 = null; foreach (var type in types) { if (isPair) { type1 = type; isPair = false; } else { type2 = type; pairs.Add(type1, type2); isPair = true; } } #endregion #region 2.本地与数据库融合 using (var db = new DapperExContext()) { var data = db.Query <SysServerInfoMst>().ToList(); // 【删除】找出DB中存在,实际接口不存在的数据,并删除DB的该条记录 foreach (var item in data) { if (pairs.Count(x => x.Key.Name == item.IntfName && x.Value.Name == item.ImplName) == 0) { var delete = db.Delete <SysServerInfoMst>(x => x.IntfName == item.IntfName && x.ImplName == item.ImplName); } } // 【新增】数据到DB、【设置】DB数据到本地 foreach (var item in pairs) { ServiceInfo serviceInfo = new ServiceInfo { IntfType = item.Key, ImplType = item.Value, LogAction = Console.WriteLine }; var tmpPairs = data.Where(x => x.IntfName == item.Key.Name && x.ImplName == item.Value.Name).ToList(); if (tmpPairs.Count != 0) { serviceInfo.Config = new ServiceConfig { IP = tmpPairs.FirstOrDefault().Ip, Port = tmpPairs.FirstOrDefault().Port.ToString(), Endpoint = tmpPairs.FirstOrDefault().Endpoint, ServiceName = tmpPairs.FirstOrDefault().ServiceName }; } else { serviceInfo.Config = new ServiceConfig { IP = ip, Port = port.ToString(), Endpoint = endpoint, ServiceName = item.Value.Name + "服务接口" }; SysServerInfoMst sysServerInfo = new SysServerInfoMst { IntfName = item.Key.Name, ImplName = item.Value.Name, ServiceName = item.Value.Name + "服务接口", RowVersion = 1, Status = "1", CreateUser = "******", UpdateUser = "******", CreateTime = DateTime.Now, UpdateTime = DateTime.Now, Ip = "localhost", Port = 1028, Endpoint = "json" }; var insert = db.Insert <SysServerInfoMst>(sysServerInfo); } serviceInfoList.Add(serviceInfo); } } #endregion return(serviceInfoList); }