Esempio n. 1
0
        private void CreateContext()
        {
            lock (_staticLock)
            {
                if (_typeToTableMap.ContainsKey(_classType))
                {
                    _tableName = _typeToTableMap[_classType];
                    _context   = _typeToContextMap[_classType];
                }
                else
                {
                    MapToAttribute mapToAttribute = ClassType.Inspector().GetAttribute <MapToAttribute>(true);
                    DefaultSortExpressionAttribute sortAttribute = ClassType.Inspector().GetAttribute <DefaultSortExpressionAttribute>(true);

                    if (mapToAttribute == null)
                    {
                        throw new CSException(string.Format("No MapTo() attribute defined for class {0}", ClassType.Name));
                    }

                    _tableName = mapToAttribute.Name;
                    _context   = mapToAttribute.Context;

                    if (sortAttribute != null)
                    {
                        _defaultSortExpression = sortAttribute.Expression;
                    }
                }
            }

            if (_context == null)
            {
                _context = CSConfig.DEFAULT_CONTEXTNAME;
            }
        }
Esempio n. 2
0
        internal CSSchemaField(PropertyInfo propInfo, CSSchema schema)
        {
            _propertyInfo = propInfo;
            _schema       = schema;
            _fieldType    = _propertyInfo.PropertyType;
            _realType     = _fieldType.Inspector().RealType;

            RelationAttribute attRelation = (RelationAttribute)Attribute.GetCustomAttribute(propInfo, typeof(RelationAttribute), true);

            _prefetch = propInfo.IsDefined(typeof(PrefetchAttribute), true);

            if (attRelation != null)
            {
                _relation = new CSRelation(schema, attRelation);

                return;
            }

            _lazy            = propInfo.IsDefined(typeof(LazyAttribute), true);
            _noCreate        = propInfo.IsDefined(typeof(NoCreateAttribute), true);
            _trim            = propInfo.IsDefined(typeof(TrimAttribute), true);
            _optimisticLock  = propInfo.IsDefined(typeof(OptimisticLockAttribute), true);
            _clientGenerated = propInfo.IsDefined(typeof(ClientGeneratedAttribute), true);
            _serverGenerated = propInfo.IsDefined(typeof(ServerGeneratedAttribute), true);
            _notMapped       = propInfo.IsDefined(typeof(NotMappedAttribute), true);


            MapToAttribute     attMapTo    = (MapToAttribute)Attribute.GetCustomAttribute(propInfo, typeof(MapToAttribute), true);
            NullValueAttribute attNull     = (NullValueAttribute)Attribute.GetCustomAttribute(propInfo, typeof(NullValueAttribute), true);
            IdentityAttribute  attIdentity = (IdentityAttribute)Attribute.GetCustomAttribute(propInfo, typeof(IdentityAttribute), true);

            if (!_notMapped)
            {
                if (CSConfig.ColumnMappingOverrideMap.ContainsValue(propInfo.DeclaringType.Name + ":" + propInfo.Name))
                {
                    _mappedColumn =
                        schema.Columns[
                            CSConfig.ColumnMappingOverrideMap[propInfo.DeclaringType.Name + ":" + propInfo.Name]];
                }
                else if (attMapTo != null)
                {
                    _mappedColumn = schema.Columns[attMapTo.Name];
                }
                else
                {
                    _mappedColumn = schema.Columns[propInfo.Name];
                }

                if (_mappedColumn != null)
                {
                    _mappedColumn.MappedField = this;
                }
            }


            SequenceAttribute sequenceAttribute = (SequenceAttribute)Attribute.GetCustomAttribute(propInfo, typeof(SequenceAttribute), true);

            if (sequenceAttribute != null && _schema.DB.SupportsSequences)
            {
                _sequenceName = sequenceAttribute.SequenceName;

                if (_mappedColumn != null && sequenceAttribute.Identity)
                {
                    _mappedColumn.Identity = true;
                    _schema.IdentityColumn = _mappedColumn;
                }
            }

            if (attIdentity != null)
            {
                if (_mappedColumn != null)
                {
                    _mappedColumn.Identity = true;
                    _schema.IdentityColumn = _mappedColumn;
                }
            }

            if (attNull != null)
            {
                _nullValue = attNull.NullValue;
            }
            else
            {
                Type fieldType = FieldType;

                if (fieldType == typeof(string))
                {
                    _nullValue = String.Empty;
                }
                else if (fieldType.IsValueType)
                {
                    _nullValue = Activator.CreateInstance(fieldType);
                }
            }

            if (_mappedColumn != null && _mappedColumn.ReadOnly)
            {
                if (_propertyInfo.CanWrite)
                {
                    throw new CSException("Property [" + Name + "] for class [" + _schema.ClassType.Name + "] should be read-only");
                }
            }
        }