示例#1
0
        public DbColumnMapping Add(string columnName, string propertyName)
        {
            string delimited = string.Format(_delimiterFormatString, columnName);
            var    mapping   = new DbColumnMapping(_delimiterFormatString);

            mapping.ColumnName   = columnName;
            mapping.PropertyName = propertyName;

            // add the same instance to both dictionaries:
            this.ByColumn.Add(mapping.ColumnName, mapping);
            this.ByProperty.Add(mapping.PropertyName, mapping);
            return(mapping);
        }
示例#2
0
        public virtual DBTableMapping getTableMappingFor <T>() where T : new()
        {
            var    result        = new DBTableMapping(this.DbDelimiterFormatString);
            var    item          = new T();
            var    itemType      = item.GetType();
            var    properties    = itemType.GetProperties();
            string replaceString = "[^a-zA-Z0-9]";
            var    rgx           = new Regex(replaceString);

            // Set up the default, trying to simple map type name to table name:
            string flattenedItemTypeName = rgx.Replace(itemType.Name.ToLower(), "");
            string plural      = Inflector.Pluralize(flattenedItemTypeName);
            var    dbTableName = this.DbTableNames.FirstOrDefault(t => rgx.Replace(t.ToLower(), "") == flattenedItemTypeName);

            if (dbTableName == null)
            {
                dbTableName = this.DbTableNames.FirstOrDefault(t => rgx.Replace(t.ToLower(), "") == plural);
            }
            // Override the default if the user specified a name with an attribute:
            var tableNameAttribute = itemType.GetTypeInfo().GetCustomAttributes(false).FirstOrDefault(a => a.GetType() == typeof(DbTableAttribute)) as DbTableAttribute;

            if (tableNameAttribute != null)
            {
                dbTableName = tableNameAttribute.Name;
            }
            // If it's still null, there is no mapping found:
            if (dbTableName == null)
            {
                throw new Exception(string.Format("Could not map class '{0}' to a table in the database.", itemType.Name));
            }
            result.DBTableName    = dbTableName;
            result.MappedTypeName = itemType.Name;
            var dbColumnInfo = from c in this.DbColumnsList where c.TableName == dbTableName select c;

            foreach (var property in properties)
            {
                var             propertyType          = property.PropertyType;
                string          flattenedPropertyName = rgx.Replace(property.Name.ToLower(), "");
                DbColumnMapping columnMapping         = dbColumnInfo.FirstOrDefault(c => rgx.Replace(c.ColumnName.ToLower(), "") == flattenedPropertyName);
                if (columnMapping != null)
                {
                    columnMapping.PropertyName = property.Name;
                    columnMapping.DataType     = propertyType;
                }
                // Look for a custom column name attribute:
                DbColumnAttribute mappedColumnAttribute = null;
                var attribute = property.GetCustomAttributes(false).FirstOrDefault(a => a.GetType() == typeof(DbColumnAttribute));
                if (attribute != null)
                {
                    // Use the column name found in the attribute:
                    mappedColumnAttribute = attribute as DbColumnAttribute;
                    string matchColumnName = mappedColumnAttribute.Name;
                    columnMapping = dbColumnInfo.FirstOrDefault(c => c.ColumnName == matchColumnName);
                    columnMapping.PropertyName = property.Name;
                    columnMapping.DataType     = propertyType;
                }
                if (columnMapping != null)
                {
                    result.ColumnMappings.Add(columnMapping);
                    if (columnMapping.IsPrimaryKey)
                    {
                        result.PrimaryKeyMapping.Add(columnMapping);
                    }
                }
            }
            if (result.PrimaryKeyMapping.Count == 0)
            {
                string keyNotDefinedMessageFormat = ""
                                                    + "No primary key mapping found in table '{0}' for type '{1}'. "
                                                    + "Please define a property which maps to a table primary key for objects of this type.";
                throw new Exception(string.Format(keyNotDefinedMessageFormat, dbTableName, itemType.Name));
            }
            return(result);
        }
示例#3
0
 public DbColumnMapping Add(DbColumnMapping mapping)
 {
     this.ByColumn.Add(mapping.ColumnName, mapping);
     this.ByProperty.Add(mapping.PropertyName, mapping);
     return(mapping);
 }