Esempio n. 1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="isGeneric"></param>
        /// <param name="type"></param>
        /// <param name="columns"></param>
        /// <param name="mapper"></param>
        internal DataContractKey(bool isGeneric, Type type, string columns, SqlNamingMapper mapper)
        {
            IsGeneric = isGeneric;

            foreach (var attr in type
#if !NETFRAMEWORK
                     .GetTypeInfo()
#endif
                     .GetCustomAttributes(false))
            {
                if (attr is DatabaseTableAttribute)
                {
                    DatabaseTableSettings = (DatabaseTableAttribute)attr;
                }
            }

            // TO DO: Should we really be calling the mapper in the data contract *key* constructor?
            // (Whatever calls are made here are called *every time* we check whether we already have a contract.)
            if (DatabaseTableSettings == null)
            {
                // we don't ever need to look up the mapper values if they have been overridden by the user-attribute;
                // these will be from the user-mapper if defined, or the default mapper if not
                DatabaseTableSettings = new DatabaseTableAttribute(
                    mapper.TableNameMapping(type),
                    mapper.CaseSensitiveColumns(type),
                    mapper.AutoMap(type));
            }

            HasMapperColumnsMapping =
                mapper.ColumnNameMapping != SqlNamingMapper.IdentityColumnMapping ||
                mapper.ColumnDataDirection != SqlNamingMapper.ColumnDataDirectionUnspecified ||
                mapper.IgnoreColumn != SqlNamingMapper.NeverIgnoreColumn;

            // If the user is trying to map column names in a dynamic instance of Mighty, then there must be a columns spec and columns auto-mapping must be left on
            if (!IsGeneric && HasMapperColumnsMapping)
            {
                if (columns == null || columns == "*")
                {
                    throw new InvalidOperationException($"You must provide an explicit `columns` specification to any dynamic instance of {nameof(MightyOrm)} with column name mapping");
                }
                if ((DatabaseTableSettings.AutoMap & AutoMap.Columns) == 0)
                {
                    throw new InvalidOperationException($"You must enable {nameof(AutoMap)}.{nameof(AutoMap.Columns)} in your {nameof(DatabaseTableSettings.AutoMap)} settings for any dynamic instance of {nameof(MightyOrm)} with column name mapping");
                }
                // Columns is not needed in the data contract except if we're here;
                // where needed, normalise it to improve caching
                DynamicColumnSpec = NormaliseColumns(columns);
            }

            ColumnName          = mapper.ColumnNameMapping;
            ColumnDataDirection = mapper.ColumnDataDirection;
            IgnoreColumn        = mapper.IgnoreColumn;

            DataItemType = type;

            DynamicNullContract = !IsGeneric && DynamicColumnSpec == null;
        }
Esempio n. 2
0
        /// <summary>
        /// 获取实体类映射表格名称
        /// </summary>
        /// <param name="entityType">实体类型</param>
        /// <returns>表格名称</returns>
        internal static String InternalGetTableName(Type entityType)
        {
            Object[] objs = entityType.GetCustomAttributes(TypeOfDatabaseTableAttribute, true);

            foreach (Object obj in objs)
            {
                DatabaseTableAttribute attr = obj as DatabaseTableAttribute;

                if (attr != null)
                {
                    return(attr.TableName);
                }
            }

            return(String.Empty);
        }