public override DbTypeAttribute GetDbType(TypeExtension typeExtension, MemberAccessor member, out bool isSet) { var extList = typeExtension[member.Name]["DbType"]; if (extList != AttributeExtensionCollection.Null) { isSet = true; var attr = new DbTypeAttribute(DbType.String); var extDbType = extList.FirstOrDefault(x => x.Name == "DbType"); var extSize = extList.FirstOrDefault(x => x.Name == "Size"); DbType dbType; if (extDbType != null) { #if SILVERLIGHT || FW4 DbType.TryParse(extDbType.Value.ToString(), out dbType); #else dbType = (DbType)Enum.Parse(typeof(DbType), extDbType.Value.ToString()); #endif attr.DbType = dbType; } if (extSize != null) { attr.Size = int.Parse(extSize.Value.ToString()); } return(attr); } return(base.GetDbType(typeExtension, member, out isSet)); }
public void ConfigureFromCustomAttribute_SetsDbTypeAccordingTo_GivenDbTypeAttribute() { var typeHandler = new TestSmartEnumTypeHandler { DbType = null }; var attribute = new DbTypeAttribute(DbType.String); typeHandler.ConfigureFromCustomAttribute(attribute) .Should().BeTrue(); typeHandler.DbType.Should().Be(DbType.String); }
/// <summary> /// 获取一个参数的值 /// </summary> /// <param name="p"></param> /// <returns></returns> object GetParameterValue(IDbDataParameter p, PropertyInfo pinfo) { DbTypeAttribute dba = pinfo.GetAttribute <DbTypeAttribute>(); if (dba == null) { return(p.Value.ToString()); } switch (dba.DbType) { case DbType.Time: case DbType.DateTime: case DbType.Date: case DbType.DateTime2: case DbType.DateTimeOffset: { return(p.Value.ToString().ToDateTime()); } case DbType.Int16: case DbType.Int32: case DbType.Int64: case DbType.UInt16: case DbType.UInt32: case DbType.UInt64: case DbType.Byte: { return(p.Value.ToString().ToInt()); } case DbType.Decimal: case DbType.Double: { return(p.Value.ToString().ToDouble()); } default: { throw new DataBaseException("字段类型" + dba.DbType + "还没有对应处理程序"); } } }
protected override IDbDataParameter GetDbDataParameter(PropertyInfo p, EntityBase info) { OracleParameter resp; DbTypeAttribute dba = p.GetAttribute <DbTypeAttribute>(); if (dba == null) { resp = new OracleParameter(p.Name, OracleDbType.Varchar2); resp.Value = p.GetValue(info, null); } else { switch (dba.DbType) { case DbType.DateTime: case DbType.Date: case DbType.DateTime2: case DbType.DateTimeOffset: { resp = new OracleParameter(p.Name, OracleDbType.Date); string value = p.GetValue(info, null)?.ToString(); if (value == null || string.IsNullOrEmpty(value.ToString())) { resp.Value = DBNull.Value; } else { resp.Value = value.ToDateTime(true); } break; } default: { throw new DataBaseException("字段类型" + dba.DbType + "还没有对应处理程序"); } } } return(resp); }
DbParameter GetDbDataParameter(PropertyInfo p, EntityBase info) { DbTypeAttribute dba = p.GetAttribute <DbTypeAttribute>(); return(GetParameter(p.Name, p.GetValue(info, null), dba?.DbType)); }
public virtual void Init(MappingSchema mappingSchema, Type type) { if (type == null) { throw new ArgumentNullException("type"); } _typeAccessor = TypeAccessor.GetAccessor(type); _mappingSchema = mappingSchema; _extension = TypeExtension.GetTypeExtension(_typeAccessor.OriginalType, mappingSchema.Extensions); _inheritanceMapping.AddRange(GetInheritanceMapping()); foreach (MemberAccessor ma in _typeAccessor) { Association a = GetAssociation(ma); if (a != null) { _associations.Add(a); continue; } if (GetMapIgnore(ma)) { continue; } MapFieldAttribute mapFieldAttr = ma.GetAttribute <MapFieldAttribute>(); if (mapFieldAttr == null || (mapFieldAttr.OrigName == null && mapFieldAttr.Format == null)) { MapMemberInfo mi = new MapMemberInfo(); DbTypeAttribute dbTypeAttribute = ma.GetAttribute <DbTypeAttribute>(); if (dbTypeAttribute != null) { mi.DbType = dbTypeAttribute.DbType; mi.IsDbTypeSet = true; if (dbTypeAttribute.Size != null) { mi.DbSize = dbTypeAttribute.Size.Value; mi.IsDbSizeSet = true; } } mi.MemberAccessor = ma; mi.Type = ma.Type; mi.MappingSchema = mappingSchema; mi.MemberExtension = _extension[ma.Name]; mi.Name = GetFieldName(ma); mi.MemberName = ma.Name; mi.Storage = GetFieldStorage(ma); mi.IsInheritanceDiscriminator = GetInheritanceDiscriminator(ma); mi.Trimmable = GetTrimmable(ma); mi.SqlIgnore = GetSqlIgnore(ma); mi.MapValues = GetMapValues(ma); mi.DefaultValue = GetDefaultValue(ma); mi.Nullable = GetNullable(ma); mi.NullValue = GetNullValue(ma, mi.Nullable); Add(CreateMemberMapper(mi)); } else if (mapFieldAttr.OrigName != null) { EnsureMapper(mapFieldAttr.MapName, ma.Name + "." + mapFieldAttr.OrigName); } else if (mapFieldAttr.Format != null) { foreach (MemberMapper inner in _mappingSchema.GetObjectMapper(ma.Type)) { EnsureMapper(string.Format(mapFieldAttr.Format, inner.Name), ma.Name + "." + inner.MemberName); } } } foreach (AttributeExtension ae in _extension.Attributes["MapField"]) { string mapName = (string)ae["MapName"]; string origName = (string)ae["OrigName"]; if (mapName == null || origName == null) { throw new MappingException(string.Format( "Type '{0}' has invalid extension. MapField MapName='{1}' OrigName='{2}'.", type.FullName, mapName, origName)); } EnsureMapper(mapName, origName); } MetadataProvider.EnsureMapper(TypeAccessor, MappingSchema, EnsureMapper); }