Exemplo n.º 1
0
        public ColumnDescriptor(MappingSchema mappingSchema, ColumnAttribute columnAttribute,
                                MemberAccessor memberAccessor)
        {
            MappingSchema  = mappingSchema;
            MemberAccessor = memberAccessor;
            MemberInfo     = memberAccessor.MemberInfo;

            if (MemberInfo.IsFieldEx())
            {
                var fieldInfo = (FieldInfo)MemberInfo;
                MemberType = fieldInfo.FieldType;
            }
            else if (MemberInfo.IsPropertyEx())
            {
                var propertyInfo = (PropertyInfo)MemberInfo;
                MemberType = propertyInfo.PropertyType;
            }

            MemberName      = columnAttribute.MemberName ?? MemberInfo.Name;
            ColumnName      = columnAttribute.Name ?? MemberInfo.Name;
            Transparent     = columnAttribute.Transparent;
            Storage         = columnAttribute.Storage;
            PrimaryKeyOrder = columnAttribute.PrimaryKeyOrder;
            IsDiscriminator = columnAttribute.IsDiscriminator;
            DataType        = columnAttribute.DataType;
            DbType          = columnAttribute.DbType;
            CreateFormat    = columnAttribute.CreateFormat;

            if (columnAttribute.HasLength())
            {
                Length = columnAttribute.Length;
            }
            if (columnAttribute.HasPrecision())
            {
                Precision = columnAttribute.Precision;
            }
            if (columnAttribute.HasScale())
            {
                Scale = columnAttribute.Scale;
            }

            var defaultCanBeNull = false;

            if (columnAttribute.HasCanBeNull())
            {
                CanBeNull = columnAttribute.CanBeNull;
            }
            else
            {
                var na = mappingSchema.GetAttribute <NullableAttribute>(MemberInfo, attr => attr.Configuration);

                if (na != null)
                {
                    CanBeNull = na.CanBeNull;
                }
                else
                {
                    CanBeNull        = mappingSchema.GetCanBeNull(MemberType);
                    defaultCanBeNull = true;
                }
            }

            if (columnAttribute.HasIsIdentity())
            {
                IsIdentity = columnAttribute.IsIdentity;
            }
            else
            {
                var a = mappingSchema.GetAttribute <IdentityAttribute>(MemberInfo, attr => attr.Configuration);
                if (a != null)
                {
                    IsIdentity = true;
                }
            }

            SkipOnInsert = columnAttribute.HasSkipOnInsert() ? columnAttribute.SkipOnInsert : IsIdentity;
            SkipOnUpdate = columnAttribute.HasSkipOnUpdate() ? columnAttribute.SkipOnUpdate : IsIdentity;

            if (defaultCanBeNull && IsIdentity)
            {
                CanBeNull = false;
            }

            if (columnAttribute.HasIsPrimaryKey())
            {
                IsPrimaryKey = columnAttribute.IsPrimaryKey;
            }
            else
            {
                var a = mappingSchema.GetAttribute <PrimaryKeyAttribute>(MemberInfo, attr => attr.Configuration);

                if (a != null)
                {
                    IsPrimaryKey    = true;
                    PrimaryKeyOrder = a.Order;
                }
            }
        }