/// <summary> /// Gets the <see cref="DataModelColumnAttribute"/> having the /// specified name and naming type. /// </summary> /// <param name="propertyNameOrColumnName"></param> /// <returns></returns> public DataModelColumnAttribute this[string propertyNameOrColumnName] { get { if (FieldMappings.ContainsKey(propertyNameOrColumnName)) { return(FieldMappings[propertyNameOrColumnName]); } var ret = GetFieldMappingByDbColumnName(propertyNameOrColumnName); if (ret != null) { return(ret); } if (ForeignModelMappings.ContainsKey(propertyNameOrColumnName)) { if (FieldMappings.ContainsKey(ForeignModelMappings[propertyNameOrColumnName].TargetMember.Name)) { return(FieldMappings[ForeignModelMappings[propertyNameOrColumnName].TargetMember.Name]); } var foreignMapping = ForeignModelMappings[propertyNameOrColumnName]; var mapping = FieldMappings.ToList().Find(fm => fm.Value.ColumnName == foreignMapping.LocalColumn).Value; return(mapping); } return(null); } }
private void LoadForeignKeyMappingAttributes() { var hierarchy = new List <Type>(); var t = this.EntityType; while (t != typeof(DataModel) && t != typeof(object)) { hierarchy.Insert(0, t); t = t.BaseType; } // walk up hierarchy foreach (var type in hierarchy) { var pis = type.GetProperties(); var fis = type.GetFields(); var mis = new Dictionary <MemberInfo, Type>(); foreach (var fi in fis) { mis.Add(fi, fi.FieldType); // fee fi fo fum } foreach (var pi in pis) { mis.Add(pi, pi.PropertyType); } foreach (var mi_kvp in mis) { var mi = mi_kvp.Key; var miType = mi_kvp.Value; var attrs = mi.GetCustomAttributes(typeof(ForeignKeyAttribute), false); foreach (ForeignKeyAttribute attr in attrs) { attr.TargetMember = mi; attr.TargetMemberType = miType; if (!FieldMappings.ContainsKey(mi.Name)) { FieldMappings[mi.Name] = new DataModelColumnAttribute(attr.ForeignColumn); } if (FieldMappings[mi.Name].ForeignKeyMapping != null && !attr.ClearBaseObjectMapping) { attr.CopyDeltaTo(FieldMappings[mi.Name].ForeignKeyMapping); } else { FieldMappings[mi.Name].ForeignKeyMapping = attr; } } } } }
public override string MapFieldType(FieldType type, DataFieldInfo dfi) { if (type == FieldType.UserString) { return(dfi.DataFieldString); } else { if (dfi.DataFieldType != FieldType.Default) { FieldMapInfo info = FieldMappings[dfi.DataFieldType]; return(string.Format(info.TypeString, ResolveLength(dfi.FieldLength, info.DefaultLength))); } else if (FieldMappings.ContainsKey(type)) { FieldMapInfo info = FieldMappings[type]; return(string.Format(info.TypeString, ResolveLength(dfi.FieldLength, info.DefaultLength))); } else { return("nvarchar(200)"); } } }
private void LoadFieldMappingAttributes() { var hierarchy = new List <Type>(); var t = this.EntityType; while (t != typeof(DataModel) && t != typeof(object)) { hierarchy.Insert(0, t); t = t.BaseType; } // walk up hierarchy foreach (var type in hierarchy) { var pis = type.GetProperties(); var fis = type.GetFields(); var mis = new Dictionary <MemberInfo, Type>(); foreach (var fi in fis) { mis.Add(fi, fi.FieldType); // fee fi fo fum } foreach (var pi in pis) { mis.Add(pi, pi.PropertyType); } foreach (var mi_kvp in mis) { var mi = mi_kvp.Key; var miType = mi_kvp.Value; var attrs = mi.GetCustomAttributes(typeof(DataModelColumnAttribute), false); if (!TypeIsFieldMappable(miType)) { if (attrs.Length > 0) { throw new InvalidAttributeException("Cannot apply a " + typeof(DataModelColumnAttribute).Name + " to a member having a custom or complex type. " + "Use " + typeof(ForeignDataModelAttribute).Name + ".", mi.Name, (Attribute)attrs[0]); } continue; } foreach (DataModelColumnAttribute attr in attrs) { attr.TargetMember = mi; attr.TargetMemberType = miType; if (string.IsNullOrEmpty(attr.ColumnName)) { attr.ColumnName = mi.Name; attr.IsColumnNameInferred = true; } if (FieldMappings.ContainsKey(mi.Name) && !attr.ClearBaseObjectMapping) { attr.CopyDeltaTo(FieldMappings[mi.Name]); } else { FieldMappings[mi.Name] = attr; } var fm = FieldMappings[mi.Name]; if (fm.DataType == null) { var def = miType; Type[] genargs; if (def.IsGenericType && (genargs = def.GetGenericArguments()).Length == 1) { def = genargs[0]; } try { fm.DbType = DbTypeConverter.ToDbType(def); } catch { } } } } } if ((FieldMappings.Count == 0 && TableMapping.PropertyLoadBehavior == InferProperties.OnlyAttributedOrAllIfNoneHaveAttributes) || TableMapping.PropertyLoadBehavior == InferProperties.NotIgnored) { foreach (var type in hierarchy) { var pis = type.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance); var fis = type.GetFields(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance); var mis = new Dictionary <MemberInfo, Type>(); foreach (var fi in fis) { mis.Add(fi, fi.FieldType); // fee fi fo fum } foreach (var pi in pis) { mis.Add(pi, pi.PropertyType); } foreach (var mi_kvp in mis) { var mi = mi_kvp.Key; if (!FieldMappings.ContainsKey(mi.Name)) { var miType = mi_kvp.Value; var ignoreAttrib = mi.GetCustomAttributes(typeof(DataModelIgnoreAttribute), false); if (ignoreAttrib != null && ignoreAttrib.Length > 0) { continue; } var gt = miType; if (!TypeIsFieldMappable(gt)) { continue; } var attr = new DataModelColumnAttribute(mi.Name); attr.TargetMember = mi; attr.TargetMemberType = miType; FieldMappings[mi.Name] = attr; var fm = attr; if (fm.DataType == null) { var def = miType; Type[] genargs; if (def.IsGenericType && (genargs = def.GetGenericArguments()).Length == 1) { def = genargs[0]; } try { fm.DbType = DbTypeConverter.ToDbType(def); } catch { } } } } } } }