private void AutoMap() { Type type = typeof(T); string columnPrefix = string.Empty; if (Attribute.IsDefined(type, typeof(PrefixForColumnsAttribute))) { columnPrefix = ((PrefixForColumnsAttribute)type.GetCustomAttribute(typeof(PrefixForColumnsAttribute))).Prefix; } foreach (PropertyInfo propertyInfo in EntityType.GetProperties()) { PropertyMap propertyMap; if (Attribute.IsDefined(propertyInfo, typeof(IgnoreAttribute))) { propertyMap = Map(propertyInfo, false).Ignore(); } else if (Attribute.IsDefined(propertyInfo, typeof(ColumnNameAttribute))) { propertyMap = Map(propertyInfo, false).Column(((ColumnNameAttribute)propertyInfo.GetCustomAttribute(typeof(ColumnNameAttribute))).Name); } ////else if (Attribute.IsDefined(propertyInfo, typeof(MapToAttribute))) //// propertyMap = Map(propertyInfo, false).Column(((MapToAttribute)propertyInfo.GetCustomAttribute(typeof(MapToAttribute))).DatabaseColumn); else if (!string.IsNullOrEmpty(columnPrefix)) { propertyMap = Map(propertyInfo, false).Column(string.Format("{0}{1}", columnPrefix, propertyInfo.Name)); } else { propertyMap = Map(propertyInfo, false); } if (Properties.Any(e => e.KeyType != KeyType.NotAKey)) { continue; } if (Attribute.IsDefined(propertyInfo, typeof(PrymaryKeyAttribute))) { propertyMap.Key(KeyType.PrimaryKey); } if (Attribute.IsDefined(propertyInfo, typeof(IdentityAttribute))) { propertyMap.Key(KeyType.Identity); } if (!string.IsNullOrEmpty(_identityColumn) && string.Equals(propertyMap.PropertyInfo.Name, _identityColumn, StringComparison.OrdinalIgnoreCase)) { propertyMap.Key(PropertyTypeKeyTypeMapping.ContainsKey(propertyMap.PropertyInfo.PropertyType) ? PropertyTypeKeyTypeMapping[propertyMap.PropertyInfo.PropertyType] : KeyType.Assigned); } } }
/// <summary> /// 重写字段映射规则 /// </summary> /// <param name="canMap"></param> protected override void AutoMap(Func <Type, PropertyInfo, bool> canMap) { Type type = typeof(T); bool hasDefinedKey = Properties.Any(p => p.KeyType != KeyType.NotAKey); PropertyMap keyMap = null; foreach (var propertyInfo in type.GetProperties()) { if (Properties.Any(p => p.Name.Equals(propertyInfo.Name, StringComparison.InvariantCultureIgnoreCase))) { continue; } if ((canMap != null && !canMap(type, propertyInfo))) { continue; } PropertyMap map = Map(propertyInfo); //标识列属性,如果存在此属性则忽略后续规则 var attrArray = propertyInfo.GetCustomAttributes(typeof(CY_System.DomainStandard.IdentityAttribute), false); if (attrArray.Length > 0) { keyMap = map; continue; } //如果找不到标识列属性,就去找一个包含ID串的属性 if (!hasDefinedKey) { if (string.Equals(map.PropertyInfo.Name, "id", StringComparison.InvariantCultureIgnoreCase)) { keyMap = map; } if (keyMap == null && map.PropertyInfo.Name.EndsWith("id", true, CultureInfo.InvariantCulture)) { keyMap = map; } } } if (keyMap != null) { keyMap.Key(PropertyTypeKeyTypeMapping.ContainsKey(keyMap.PropertyInfo.PropertyType) ? PropertyTypeKeyTypeMapping[keyMap.PropertyInfo.PropertyType] : KeyType.Assigned); } }
protected override void AutoMap(Func <Type, PropertyInfo, bool> canMap) { Type type = typeof(T); bool hasDefinedKey = Properties.Any(p => p.KeyType != KeyType.NotAKey); PropertyMap keyMap = null; foreach (var propertyInfo in type.GetProperties()) { if (Properties.Any(p => p.Name.Equals(propertyInfo.Name, StringComparison.CurrentCultureIgnoreCase))) { continue; } if ((canMap != null && !canMap(type, propertyInfo))) { continue; } if (IsAssignableToGenericType(propertyInfo.PropertyType, typeof(ICollection <>))) { continue; } if (typeof(IEntity).IsAssignableFrom(propertyInfo.PropertyType)) { continue; } PropertyMap map = Map(propertyInfo); if (!hasDefinedKey) { if (string.Equals(map.PropertyInfo.Name, "id", StringComparison.CurrentCultureIgnoreCase)) { keyMap = map; } // if (keyMap == null && map.PropertyInfo.Name.EndsWith("id", true, CultureInfo.InvariantCulture)) if (keyMap == null && map.PropertyInfo.Name.EndsWith("id", StringComparison.CurrentCultureIgnoreCase)) { keyMap = map; } } } if (keyMap != null) { keyMap.Key(PropertyTypeKeyTypeMapping.ContainsKey(keyMap.PropertyInfo.PropertyType) ? PropertyTypeKeyTypeMapping[keyMap.PropertyInfo.PropertyType] : KeyType.Assigned); } }
protected override void AutoMap(Func <Type, PropertyInfo, bool> canMap) { Type type = typeof(T); bool hasDefinedKey = Properties.Any(p => p.KeyType != KeyType.NotAKey); PropertyMap keyMap = null; foreach (var propertyInfo in type.GetProperties()) { ColumnAttribute column = propertyInfo.GetCustomAttribute <ColumnAttribute>(); if (column == null) { continue; } if (Properties.Any(p => p.Name.Equals(propertyInfo.Name, StringComparison.InvariantCultureIgnoreCase))) { continue; } if ((canMap != null && !canMap(type, propertyInfo))) { continue; } var map = Map(propertyInfo).Column(column.Name); if (!hasDefinedKey) { if (string.Equals(map.PropertyInfo.Name, "id", StringComparison.InvariantCultureIgnoreCase)) { keyMap = map; } if (propertyInfo.GetCustomAttributes <KeyAttribute>() != null) { // 或者是有KEY keyMap = map; } } } if (keyMap != null && !_isManualID) { keyMap.Key(PropertyTypeKeyTypeMapping.ContainsKey(keyMap.PropertyInfo.PropertyType) ? PropertyTypeKeyTypeMapping[keyMap.PropertyInfo.PropertyType] : KeyType.Assigned); } }
private void CustomAutoMap(Func<Type, PropertyInfo, bool> canMap) { Type type = typeof(T); bool hasDefinedKey = Properties.Any(p => p.KeyType != KeyType.NotAKey); PropertyMap keyMap = null; foreach (var propertyInfo in type.GetProperties()) { // Exclude virtual properties if (propertyInfo.GetGetMethod().IsVirtual) { continue; } if (Properties.Any(p => p.Name.Equals(propertyInfo.Name, StringComparison.InvariantCultureIgnoreCase))) { continue; } if ((canMap != null && !canMap(type, propertyInfo))) { continue; } PropertyMap map = Map(propertyInfo); if (!hasDefinedKey) { if (string.Equals(map.PropertyInfo.Name, "id", StringComparison.InvariantCultureIgnoreCase)) { keyMap = map; } if (keyMap == null && map.PropertyInfo.Name.EndsWith("id", true, CultureInfo.InvariantCulture)) { keyMap = map; } } } if (keyMap != null) { keyMap.Key(PropertyTypeKeyTypeMapping.ContainsKey(keyMap.PropertyInfo.PropertyType) ? PropertyTypeKeyTypeMapping[keyMap.PropertyInfo.PropertyType] : KeyType.Assigned); } }