示例#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;
        }
示例#2
0
#pragma warning restore IDE0059

        /// <summary>
        /// Is this the name of a PK field?
        /// </summary>
        /// <param name="fieldName">The name to check</param>
        /// <param name="canonicalKeyName">Returns the canonical key name, i.e. as specified in <see cref="MightyOrm"/> constructor</param>
        /// <returns></returns>
        internal bool IsKey(string fieldName, out string canonicalKeyName)
        {
            canonicalKeyName = null;
            foreach (var key in PrimaryKeyColumnList)
            {
                if (key.Equals(fieldName, SqlNamingMapper.CaseSensitiveColumns(DataItemType) ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase))
                {
                    canonicalKeyName = key;
                    return(true);
                }
            }
            return(false);
        }