예제 #1
0
 /// <summary>
 /// 添加索引
 /// </summary>
 /// <param name="table">表名</param>
 /// <param name="index">索引列信息</param>
 /// <returns>是否成功</returns>
 public abstract bool AddIndex(string table, IndexModel index);
예제 #2
0
        /// <summary>
        /// 获取列信息
        /// </summary>
        /// <param name="property">model 属性</param>
        /// <param name="table">表名</param>
        /// <returns>列信息</returns>
        public virtual ColumnInfoModel GetColumnInfo(PropertyInfo property, string table)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }
            if (string.IsNullOrEmpty(table))
            {
                throw new ArgumentNullException("table");
            }
            ColumnInfoModel m = new ColumnInfoModel();

            m.Name = property.Name;
            int maxlength = 0;
            int minlength = 0;

            GetColumnInLength(property, out maxlength, out minlength);
            Type propertyType = property.PropertyType;

            m.IsNullable = false;
            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                m.IsNullable = true;
                propertyType = propertyType.GetGenericArguments()[0];
            }
            string dataType = this.GetColumnType(propertyType, maxlength, minlength);

            if (string.IsNullOrEmpty(dataType))
            {
                throw new Exception("未找到" + property.PropertyType.FullName + "对应的数据库类型!");
            }
            m.DataType  = dataType;
            m.MaxLength = maxlength;
            m.MinLength = minlength;
            var atts = property.GetCustomAttributes(typeof(ColumnAttribute), false);

            if (atts != null && atts.Length > 0)
            {
                var att = atts[0] as ColumnAttribute;
                if (!string.IsNullOrEmpty(att.Name))
                {
                    m.Name = att.Name;
                }
            }

            m.IsAutoIncrement = false;
            if (propertyType == typeof(int) || propertyType == typeof(uint) ||
                propertyType == typeof(long) || propertyType == typeof(ulong))
            {
                atts = property.GetCustomAttributes(typeof(DatabaseGeneratedAttribute), false);
                if (atts != null && atts.Length > 0)
                {
                    var att = atts[0] as DatabaseGeneratedAttribute;
                    if (att.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity)
                    {
                        m.IsAutoIncrement = true;
                    }
                }
            }

            m.IsKey          = false;
            m.IsNonClustered = false;
            atts             = property.GetCustomAttributes(typeof(KeyAttribute), false);
            if (atts != null && atts.Length > 0)
            {
                m.IsKey          = true;
                atts             = property.GetCustomAttributes(typeof(NonClusteredAttribute), false);
                m.IsNonClustered = atts != null && atts.Length > 0;
            }
            else
            {
                if (!propertyType.IsValueType)
                {
                    m.IsNullable = true;
                    atts         = property.GetCustomAttributes(typeof(RequiredAttribute), false);
                    if (atts != null && atts.Length > 0)
                    {
                        m.IsNullable = false;
                    }
                }

                atts = property.GetCustomAttributes(typeof(IndexAttribute), false);
                if (atts != null && atts.Length > 0)
                {
                    m.Indexs = new List <IndexModel>(atts.Length);
                    foreach (var o in atts)
                    {
                        var att   = o as IndexAttribute;
                        var index = new IndexModel();
                        m.Indexs.Add(index);
                        index.ColumnName = m.Name;
                        index.IsUnique   = att.IsUnique;
                        if (!string.IsNullOrEmpty(att.Name))
                        {
                            index.Name = att.Name;
                        }
                        else
                        {
                            m.Name = string.Format("IX_{0}_{1}", table, m.Name);
                        }
                    }
                }
            }

            m.Comment = this.GetComment(property.DeclaringType.Assembly, "P:" + property.DeclaringType.FullName + "." + property.Name) ?? string.Empty;

            return(m);
        }