Example #1
0
        /// <summary>
        ///     关系映射
        /// </summary>
        /// <param name="type">实体类Type</param>
        public TableMap(Type type)
        {
            ModelList = new Dictionary <PropertyInfo, FieldMapState>();
            object[] attrs;

            //变量属性
            FieldMapState fieldMapState;

            #region 类属性

            //类属性
            ClassInfo = new DBAttribute();

            attrs     = type.GetCustomAttributes(typeof(DBAttribute), false);
            ClassInfo = attrs.Length == 0 ? new DBAttribute() : ((DBAttribute)attrs[0]);

            if (ClassInfo.Name.IsNullOrEmpty())
            {
                ClassInfo.Name = type.Name;
            }

            #region 自动创建数据库配置文件

            if (DbConfigs.ConfigInfo.DbList.Count == 0 && ClassInfo.DbIndex == 0)
            {
                var db = new DbConfig();
                db.DbList.Add(new DbInfo
                {
                    Catalog        = "数据库名称",
                    CommandTimeout = 60,
                    ConnectTimeout = 30,
                    DataType       = DataBaseType.SqlServer,
                    DataVer        = "2005",
                    PassWord       = "******",
                    PoolMaxSize    = 100,
                    PoolMinSize    = 16,
                    Server         = ".",
                    UserID         = "sa"
                });
                DbConfigs.SaveConfig(db);
            }
            else if (DbConfigs.ConfigInfo.DbList.Count - 1 < ClassInfo.DbIndex)
            {
                throw new Exception("数据库配置(索引项:" + ClassInfo.DbIndex + ")不存在!");
            }

            #endregion

            #region 获取DbConfig的配置

            DbInfo dbInfo = ClassInfo.DbIndex;
            ClassInfo.ConnStr        = DbFactory.CreateConnString(ClassInfo.DbIndex);
            ClassInfo.DataType       = dbInfo.DataType;
            ClassInfo.DataVer        = dbInfo.DataVer;
            ClassInfo.CommandTimeout = dbInfo.CommandTimeout;

            #endregion

            #endregion

            #region 变量属性

            //遍历所有属性变量,取得对应使用标记名称
            //无加标记时,则为不使用该变量。
            foreach (var propertyInfo in type.GetProperties())
            {
                fieldMapState = new FieldMapState();

                // 是否带属性
                attrs = propertyInfo.GetCustomAttributes(false);
                foreach (var item in attrs)
                {
                    // 加入属性
                    fieldMapState.IsDbField = !(item is NotJoinAttribute);
                    // 数据类型
                    if (item is DataTypeAttribute)
                    {
                        fieldMapState.DataType = (DataTypeAttribute)item; continue;
                    }
                    // 字段映射
                    if (item is ColumnAttribute)
                    {
                        fieldMapState.Column = (ColumnAttribute)item; continue;
                    }
                    // 属性扩展
                    if (item is PropertyExtendAttribute)
                    {
                        fieldMapState.PropertyExtend = ((PropertyExtendAttribute)item).PropertyExtend; continue;
                    }
                }
                //if (fieldMapState.Display == null) { fieldMapState.Display = new DisplayAttribute { Name = propertyInfo.Name }; }
                //if (fieldMapState.Display.Name.IsNullOrEmpty()) { fieldMapState.Display.Name = propertyInfo.Name; }

                if (fieldMapState.Column == null)
                {
                    fieldMapState.Column = new ColumnAttribute {
                        Name = propertyInfo.Name
                    };
                }
                if (fieldMapState.Column.Name.IsNullOrEmpty())
                {
                    fieldMapState.Column.Name = propertyInfo.Name;
                }

                if (fieldMapState.IsDbField && fieldMapState.Column.IsDbGenerated)
                {
                    IndexName = fieldMapState.Column.Name;
                }
                else
                {
                    fieldMapState.Column.IsDbGenerated = false;
                }

                //添加属变量标记名称
                ModelList.Add(propertyInfo, fieldMapState);
            }
            #endregion

            Type = type;
        }
Example #2
0
        /// <summary>
        ///     关系映射
        /// </summary>
        /// <param name="type">实体类Type</param>
        public TableMap(Type type)
        {
            Type      = type;
            ModelList = new Dictionary <PropertyInfo, FieldMapState>();

            #region 类属性
            var attrs = Type.GetCustomAttributes(typeof(DBAttribute), false);
            foreach (var attr in attrs.OfType <DBAttribute>())
            {
                ClassInfo = attr;
            }
            if (ClassInfo == null)
            {
                ClassInfo = new DBAttribute(null);
            }
            if (string.IsNullOrEmpty(ClassInfo.Name))
            {
                ClassInfo.Name = Type.Name;
            }
            #endregion

            #region 变量属性

            //遍历所有属性变量,取得对应使用标记名称
            //无加标记时,则为不使用该变量。
            foreach (var propertyInfo in Type.GetProperties())
            {
                var fieldMapState = new FieldMapState();

                // 是否带属性
                attrs = propertyInfo.GetCustomAttributes(false);
                foreach (var item in attrs)
                {
                    // 数据类型
                    if (item is DataTypeAttribute)
                    {
                        fieldMapState.DataType = (DataTypeAttribute)item; continue;
                    }
                    // 字段映射
                    if (item is ColumnAttribute)
                    {
                        fieldMapState.Column = (ColumnAttribute)item; continue;
                    }
                    // 属性扩展
                    if (item is PropertyExtendAttribute)
                    {
                        fieldMapState.PropertyExtend = ((PropertyExtendAttribute)item).PropertyExtend; continue;
                    }
                    // 存储过程参数
                    if (item is ProcAttribute)
                    {
                        fieldMapState.IsOutParam = ((ProcAttribute)item).IsOutParam; fieldMapState.IsInParam = ((ProcAttribute)item).IsInParam; continue;
                    }
                }
                //if (fieldMapState.Display == null) { fieldMapState.Display = new DisplayAttribute { Name = propertyInfo.Name }; }
                //if (fieldMapState.Display.Name.IsNullOrEmpty()) { fieldMapState.Display.Name = propertyInfo.Name; }

                if (fieldMapState.Column == null)
                {
                    fieldMapState.Column = new ColumnAttribute {
                        Name = propertyInfo.Name
                    };
                }
                if (string.IsNullOrEmpty(fieldMapState.Column.Name))
                {
                    fieldMapState.Column.Name = propertyInfo.Name;
                }
                if (fieldMapState.Column.IsMap && fieldMapState.Column.IsPrimaryKey)
                {
                    IndexName = fieldMapState.Column.Name;
                }
                else
                {
                    fieldMapState.Column.IsPrimaryKey = false;
                }

                //添加属变量标记名称
                ModelList.Add(propertyInfo, fieldMapState);
            }
            #endregion
        }