public DbMapInfo(string dbName, string netName, DataColumnAttribute attrColumn, ExtendColumnAttribute attrExtColumn, PropertyInfo prop) { this.DbName = dbName; this.NetName = netName; this.AttrColumn = attrColumn; this.PropertyInfo = prop; this.AttrExtColumn = attrExtColumn; }
public static TypeDescription GetTypeDiscription(Type type) { TypeDescription description = s_typeInfoDict[type.FullName] as TypeDescription; if (description == null) { PropertyInfo[] properties = type.GetProperties(s_flag); int length = properties.Length; Dictionary <string, DbMapInfo> dict = new Dictionary <string, DbMapInfo>(length, StringComparer.OrdinalIgnoreCase); foreach (PropertyInfo prop in properties) { DbMapInfo info = null; DataColumnAttribute attrColumn = prop.GetMyAttribute <DataColumnAttribute>(); if (attrColumn != null) { info = new DbMapInfo(string.IsNullOrEmpty(attrColumn.Alias) ? prop.Name : attrColumn.Alias, prop.Name, attrColumn, null, prop); } else { ExtendColumnAttribute attrExtColumn = prop.GetMyAttribute <ExtendColumnAttribute>(); info = new DbMapInfo(prop.Name, prop.Name, null, attrExtColumn, prop); } dict[info.DbName] = info; } description = new TypeDescription { MemberDict = dict }; // 添加到缓存字典 s_typeInfoDict[type.FullName] = description; } return(description); }
public CodeGenerator(Type entityType) { _entityType = entityType; _sb = new StringBuilder(1024 * 20); DataEntityAttribute entityAttr = _entityType.GetMyAttribute <DataEntityAttribute>(); string tableName; if (entityAttr == null) { tableName = _entityType.Name; } else { tableName = string.IsNullOrEmpty(entityAttr.Alias) ? _entityType.Name : entityAttr.Alias; } _tableInfo = new DbMapInfo(tableName, _entityType.Name, null, null, null); _allFields = new List <DbMapInfo>(20); _keyFields = new List <DbMapInfo>(3); DbMapInfo info = null; foreach (PropertyInfo property in _entityType.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { string fieldName = property.Name; ExtendColumnAttribute attrExtColumn = property.GetMyAttribute <ExtendColumnAttribute>(); // 扩展的字段跳过 by chenb02 //if (attrExtColumn != null) // continue; DataColumnAttribute attrColumn = property.GetMyAttribute <DataColumnAttribute>(); if (attrColumn != null) { if (string.IsNullOrEmpty(attrColumn.Alias) == false) { fieldName = attrColumn.Alias; } info = new DbMapInfo(fieldName, property.Name, attrColumn, attrExtColumn, property); _allFields.Add(new DbMapInfo(fieldName, property.Name, attrColumn, attrExtColumn, property)); if (attrColumn.Identity) { _identityField = info; } if (attrColumn.TimeStamp) { _timeStampField = info; } if (attrColumn.SeqGuid) { _seqGuidField = info; } if (attrColumn.PrimaryKey) { _keyFields.Add(info); } } else { _allFields.Add(new DbMapInfo(fieldName, property.Name, null, attrExtColumn, property)); } } }
/// <summary> /// 转换属性 /// </summary> /// <param name="info">属性</param> /// <param name="entity">实体Sql信息</param> private void TransferProperty(PropertyInfo info, EntitySqlInfo entity) { DataColumnAttribute attrColumn = info.GetMyAttribute <DataColumnAttribute>(); if (attrColumn != null) { var prop = ""; if (!string.IsNullOrEmpty(attrColumn.Alias)) { prop = string.Format("{0}.[{1}]", entity.TableName, attrColumn.Alias); } else { prop = string.Format("{0}.[{1}]", entity.TableName, info.Name); } entity.Properties.Add(prop); if (attrColumn.PrimaryKey) { entity.PrimaryKey = prop; } } else { ExtendColumnAttribute attrExtColumn = info.GetMyAttribute <ExtendColumnAttribute>(); if (attrExtColumn != null && attrExtColumn.ColumnType != 0) { switch (attrExtColumn.ColumnType) { case ExtendColumnTypeEnum.KeyParamOption_Code: entity.ExtendTables.Add(string.Format("LEFT JOIN dbo.KeyParamOption AS {0} ON [{0}].[Code] = {1}.[{2}] AND [{0}].[ParamGroup] = '{3}'", info.Name, entity.TableName, attrExtColumn.RelationColumn, attrExtColumn.Expression)); entity.Properties.Add(string.Format("[{0}].[Name] AS {0}", info.Name)); break; case ExtendColumnTypeEnum.KeyParamOption_Guid: entity.ExtendTables.Add(string.Format("LEFT JOIN dbo.KeyParamOption AS {0} ON [{0}].[KeyParamOptionID] = {1}.[{2}]", info.Name, entity.TableName, attrExtColumn.RelationColumn)); entity.Properties.Add(string.Format("[{0}].[Name] AS {0}", info.Name)); break; case ExtendColumnTypeEnum.UserName_Guid: entity.ExtendTables.Add(string.Format("LEFT JOIN {0}.dbo.ORG_UserInfo AS {1} ON [{1}].[UserGUID] = {2}.[{3}]", ConnStringHelper.GetDBName("SystemCore"), info.Name, entity.TableName, attrExtColumn.RelationColumn)); entity.Properties.Add(string.Format("[{0}].[UserName_Chn] AS {0}", info.Name)); break; case ExtendColumnTypeEnum.UserName_Code: entity.ExtendTables.Add(string.Format("LEFT JOIN {0}.dbo.ORG_UserInfo AS {1} ON [{1}].[UserCode] = {2}.[{3}]", ConnStringHelper.GetDBName("SystemCore"), info.Name, entity.TableName, attrExtColumn.RelationColumn)); entity.Properties.Add(string.Format("[{0}].[UserName_Chn] AS {0}", info.Name)); break; case ExtendColumnTypeEnum.BusinessUnit_Guid: entity.ExtendTables.Add(string.Format("LEFT JOIN {0}.dbo.ORG_BusinessUnit AS {1} ON [{1}].[BUGUID] = {2}.[{3}]", ConnStringHelper.GetDBName("SystemCore"), info.Name, entity.TableName, attrExtColumn.RelationColumn)); entity.Properties.Add(string.Format("[{0}].[BUName] AS {0}", info.Name)); break; case ExtendColumnTypeEnum.BusinessUnit_Code: entity.ExtendTables.Add(string.Format("LEFT JOIN {0}.dbo.ORG_BusinessUnit AS {1} ON [{1}].[BUCode] = {2}.[{3}]", ConnStringHelper.GetDBName("SystemCore"), info.Name, entity.TableName, attrExtColumn.RelationColumn)); entity.Properties.Add(string.Format("[{0}].[BUName] AS {0}", info.Name)); break; case ExtendColumnTypeEnum.Common: entity.ExtendTables.Add(attrExtColumn.Expression); entity.Properties.Add(attrExtColumn.RelationColumn); break; } } } }