Beispiel #1
0
        private static void SetFieldValue(Object pivotalObject, DataRow dataRow, EntityColumn columnInfo)
        {
            var dbValue        = dataRow[columnInfo.Name];
            var convertedValue = ConvertDatabaseValueToPropertyValue(dbValue, columnInfo);

            columnInfo.Property.SetValue(pivotalObject, convertedValue);
        }
 private EntityMetadata(string tableName, EntityColumn primaryKey, IReadOnlyCollection <EntityColumn> columns, EntityColumn foreignKey, IReadOnlyCollection <Secondary> secondaries)
 {
     TableName   = tableName;
     PrimaryKey  = primaryKey;
     Columns     = columns;
     ForeignKey  = foreignKey;
     Secondaries = secondaries;
 }
Beispiel #3
0
        private static object ConvertDatabaseValueToPropertyValue(object dbValue, EntityColumn fieldInfo)
        {
            if (dbValue is DBNull || dbValue == null)
            {
                return(null);
            }

            var targetType = fieldInfo.Property.PropertyType;

            if (targetType.IsAssignableFrom(dbValue.GetType()))
            {
                return(dbValue);
            }

            var typeToUse = Nullable.GetUnderlyingType(targetType) ?? targetType;

            if (typeToUse == typeof(bool))
            {
                var intValue = Convert.ToInt32(dbValue);
                if (intValue < 0 || intValue > 1)
                {
                    throw new MappingException(string.Format("Unable to convert value {0} to boolean", intValue));
                }

                return(intValue == 1);
            }

            if (typeToUse.IsEnum) // Pivotal Combo
            {
                var strValue = Convert.ToString(dbValue);

                try
                {
                    return(GetEnumValueFromName(typeToUse, strValue));
                }
                catch (Exception ex)
                {
                    throw new MappingException(string.Format("Unable to convert value {0} to enum {1}", strValue, typeToUse.FullName), ex);
                }
            }

            return(Convert.ChangeType(dbValue, typeToUse));
        }
Beispiel #4
0
        private static object ConvertPropertyValueToDatabaseValue(object propertyValue, EntityColumn columnInfo)
        {
            if (propertyValue == null)
            {
                return(DBNull.Value);
            }

            if (columnInfo.AsString)
            {
                //if (!(propertyValue is bool))
                //{
                //    throw new MappingException(string.Format("Unable to convert value {0} to boolean", intValue));

                //    throw new ArgumentException("Unable to apply AsString");
                //}

                return(propertyValue.ToString());
                //return (double)rawValue * 100;
            }

            var type = propertyValue.GetType();

            if (type.IsEnum)
            {
                var attribute = type.GetField(Enum.GetName(type, propertyValue))
                                .GetCustomAttributes(typeof(DisplayAttribute), false)
                                .SingleOrDefault() as DisplayAttribute;

                return((attribute != null && attribute.Name != "")
                    ? attribute.Name
                    : propertyValue.ToString());
            }

            if (!(propertyValue is string))
            {
                return(propertyValue);
            }

            var stringValue = propertyValue as string;

            return(stringValue != ""
                ? stringValue.ToANSI()
                : null);
        }