Example #1
0
 internal void AddMember(MemberMapInfo member)
 {
     members.Add(member);
 }
 internal void AddMember(MemberMapInfo member)
 {
     members.Add(member);
 }
Example #3
0
        private static MemberMapInfo GetMemberMapInfo(TypeMapInfo typeMapInfo, MemberInfo memberInfo)
        {
            string  columnName = memberInfo.Name;
            SqlType columnType = null;

            bool canBeNull = CanBeNull(memberInfo);
            bool nullable  = canBeNull;

            object defaultValue        = null;
            bool   defaultIsExpression = false;

#if PCL
            if (memberInfo.IsDefined(typeof(ColumnAttribute)))
            {
                var attribute = memberInfo.GetCustomAttribute <ColumnAttribute>();
#else
            if (Attribute.IsDefined(memberInfo, typeof(ColumnAttribute)))
            {
                var attribute = (ColumnAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(ColumnAttribute));
#endif
                if (!String.IsNullOrEmpty(attribute.Name))
                {
                    columnName = attribute.Name;
                }

                if (!String.IsNullOrEmpty(attribute.TypeName))
                {
                    if (!PrimitiveTypes.IsPrimitive(attribute.TypeName))
                    {
                        throw new NotSupportedException(String.Format("The type '{0}' is not supported.", attribute.TypeName));
                    }

                    columnType = SqlType.Parse(attribute.TypeName);
                }

                defaultValue        = attribute.Default;
                defaultIsExpression = attribute.DefaultIsExpression;

                nullable = attribute.Null;
            }

            if (nullable && !canBeNull)
            {
                throw new NotSupportedException(String.Format("The member '{0}' is marked as NULL but is not nullable.", memberInfo.Name));
            }

            if (columnType == null)
            {
                columnType = GetSqlType(memberInfo);
            }

            ConstraintMapInfo[] constraints = null;
#if PCL
            if (memberInfo.IsDefined(typeof(ConstraintAttribute)))
            {
                var attributes = memberInfo.GetCustomAttributes <ConstraintAttribute>();
#else
            if (Attribute.IsDefined(memberInfo, typeof(ConstraintAttribute)))
            {
                var attributes = (ConstraintAttribute[])Attribute.GetCustomAttributes(memberInfo, typeof(ConstraintAttribute));
#endif
                constraints = attributes.Select(x => new ConstraintMapInfo(memberInfo, columnName, x.Type, x.Expression)).ToArray();
            }

#if PCL
            if (memberInfo.IsDefined(typeof(IdentityAttribute)))
            {
#else
            if (Attribute.IsDefined(memberInfo, typeof(IdentityAttribute)))
            {
#endif
                if (constraints != null &&
                    constraints.Length > 0)
                {
                    throw new InvalidOperationException("Cannot specify an identity for a column that already defines a constraint.");
                }

                if (defaultValue != null)
                {
                    throw new InvalidOperationException("Cannot specify an identity for a column that defines a default expression.");
                }

                constraints         = new[] { new ConstraintMapInfo(memberInfo, columnName, ConstraintType.PrimaryKey, null) };
                defaultValue        = String.Format("UNIQUEKEY('{0}')", typeMapInfo.TableName);
                defaultIsExpression = true;
            }

            var mapInfo = new MemberMapInfo(memberInfo, columnName, columnType, nullable, constraints);
            if (defaultValue != null)
            {
                mapInfo.SetDefault(defaultValue, defaultIsExpression);
            }
            return(mapInfo);
        }