/// <summary> /// Load the mappings attributes for the specified type. /// </summary> /// <param name="assembly"></param> static void LoadMappings(Type type) { // Check if this mapping has already been loaded. if (_mappings.ContainsKey(type)) { return; } lock (_mappings) { // Check if this mapping has already been loaded. if (_mappings.ContainsKey(type)) { return; } object[] attributes = type.GetCustomAttributes(typeof(DataEntityAttribute), false); if (attributes.Length > 0) { DataEntityAttribute attribute = attributes[0] as DataEntityAttribute; if (attribute == null || String.IsNullOrEmpty(attribute.TableName)) { throw new Exception(String.Format("Cannot load mappings for type [{0}].", type.FullName)); } TableMapping mapping = new TableMapping(attribute, type); _mappings.Add(type, mapping); } } }
/// <summary> /// 获取实体sql信息 /// </summary> /// <returns>实体sql信息</returns> private EntitySqlInfo GetEntitySqlInfo() { EntitySqlInfo _info = (EntitySqlInfo)s_table[typeof(T).FullName]; //_info = null if (_info == null) { // 用于支持扩展列的表名 _info = new EntitySqlInfo(); //_info.TableName = string.Format("[{0}]", typeof(T).Name); _info.TableName = string.Format("{0}", typeof(T).Name); // 如果有别名,则使用别名 DataEntityAttribute attribute = typeof(T).GetMyAttribute <DataEntityAttribute>(); if (attribute != null && !string.IsNullOrEmpty(attribute.Alias)) { //_info.TableName = string.Format("[{0}]", attribute.Alias); _info.TableName = string.Format("{0}", attribute.Alias); } // 获取字段名 foreach (PropertyInfo prop in typeof(T).GetProperties()) { TransferProperty(prop, _info); } s_table[typeof(T).FullName] = _info; } return(_info); }
public TableMapping(DataEntityAttribute tableAttribute, Type type) : this() { this.Key = tableAttribute.Key; this.TableName = tableAttribute.TableName; this.SequenceName = tableAttribute.SequenceName; this.CachingEnabled = !tableAttribute.DisableCaching; this.LoadColumns(type, null); this.Initialize(); }
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); _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; DataColumnAttribute attr = property.GetMyAttribute <DataColumnAttribute>(); if (attr != null) { if (string.IsNullOrEmpty(attr.Alias) == false) { fieldName = attr.Alias; } info = new DbMapInfo(fieldName, property.Name, attr, property); _allFields.Add(new DbMapInfo(fieldName, property.Name, attr, property)); if (attr.Identity) { _identityField = info; } if (attr.TimeStamp) { _timeStampField = info; } if (attr.SeqGuid) { _seqGuidField = info; } if (attr.PrimaryKey) { _keyFields.Add(info); } } else { _allFields.Add(new DbMapInfo(fieldName, property.Name, null, property)); } } }