예제 #1
0
        /// <summary>
        /// 获取实体属性对应的数据库字段名
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public static string GetDbFieldName(this MemberInfo member)
        {
            DbColumnAttribute a = member.GetMyAttribute <DbColumnAttribute>();

            return((a != null && string.IsNullOrEmpty(a.Alias) == false)
                                        ? a.Alias
                                        : member.Name);
        }
예제 #2
0
        public ColumnInfo(PropertyInfo propertyInfo, DbColumnAttribute attr)
        {
            if (propertyInfo == null)
            {
                throw new ArgumentNullException("propertyInfo");
            }

            this.Attr         = attr;
            this.PropertyInfo = propertyInfo;
        }
예제 #3
0
        public static EntityDescription Create(Type entityType, bool checkIgnore)
        {
            // 获取所有类型的属性定义(注意:不处理Field)
            PropertyInfo[] properties = entityType.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            Dictionary <string, ColumnInfo> dict
                = new Dictionary <string, ColumnInfo>(properties.Length, StringComparer.OrdinalIgnoreCase);

            int index = -1;

            foreach (PropertyInfo prop in properties)
            {
                // 为了方便排查问题,属性的序号以出现的次序为准,
                // 如果有属性被忽略了,那么序号也累加(对于变更状态数组来说,就是浪费对应的元素)
                index++;

                if (prop.CanWrite == false)     // 不能写的属性根本不能赋值,只能排除!
                {
                    continue;
                }

                Type dataType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                // 排除不支持的数据类型(有可能是嵌套实体)
                if (IsSupportType(dataType) == false)
                {
                    continue;
                }

                if (prop.IsIndexerProperty()) // 排除索引器属性
                {
                    continue;
                }

                // 获取属性的数据库定义信息(可能为 null )
                DbColumnAttribute attr = prop.GetMyAttribute <DbColumnAttribute>();

                if (checkIgnore)
                {
                    if (attr != null && attr.Ignore)
                    {
                        continue;
                    }
                }

                ColumnInfo info = new ColumnInfo(prop, attr)
                {
                    DataType = dataType, Index = index
                };

                dict[info.PropertyInfo.Name] = info;
            }

            return(new EntityDescription {
                MemberDict = dict,
                PropertyCount = properties.Length

                                // 反射过程中,不需要实体到表结构的映射信息,所以就不读取DbEntityAttribute
                                // CodeDom过程中只使用一次,不需要缓存,所以在获取后自行读取DbEntityAttribute
                                //Attr = type.GetMyAttribute<DbEntityAttribute>()
            });
        }