Exemplo n.º 1
0
        public static String GetColoumName(Type type, String name)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Table对应的属性值不能为空");
            }
            if (!ValuePairs.ContainsKey(type))
            {
                lock (_lock)
                {
                    Add(type);
                }
            }
            EntityTableValue pairs = null;

            if (ValuePairs.TryGetValue(type, out pairs))
            {
                return(pairs[name].ColumnName);
            }
            return(null);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 添加类型,并缓存字段对应的所有属性
 /// </summary>
 /// <param name="type"></param>
 public static void Add(Type type)
 {
     if (type == null)
     {
         throw new ArgumentException("类型不能为空");
     }
     if (!typeof(IEntity).IsAssignableFrom(type))
     {
         throw new ArgumentException("必须基于IEntity接口");
     }
     if (!ValuePairs.ContainsKey(type))
     {
         lock (_lock)
         {
             if (ValuePairs.ContainsKey(type))
             {
                 return;
             }
             Type             t     = type;
             EntityTableValue value = new EntityTableValue();
             Attribute        table = t.GetCustomAttribute(typeof(TableMapperAttribute));
             //若没有写对应的特性,那么类名便是表名
             String TableName = t.Name;
             if (table != null)
             {
                 TableName = (table as TableMapperAttribute).TableName;
             }
             value.Tablename = TableName;
             //将字段与数据库中字段名对照起来
             Dictionary <String, EntityPropertyValue> valuePairs = new Dictionary <String, EntityPropertyValue>();
             foreach (PropertyInfo pi in t.GetProperties())
             {
                 if (pi.GetCustomAttribute(typeof(IgnoreAttribute)) != null)
                 {
                     continue;
                 }
                 String    name      = pi.Name;
                 Attribute attribute = pi.GetCustomAttribute(typeof(PropertyNameAttribute));
                 if (attribute != null)
                 {
                     name = ((PropertyNameAttribute)attribute).ColumnName;
                 }
                 //自增主键只允许有一个,如果想多个主键,请实现表达式树写法
                 if (String.IsNullOrEmpty(value.PkColumnName))
                 {
                     Attribute pk = pi.GetCustomAttribute(typeof(PrimaryKeyAttribute));
                     if (pk != null)
                     {
                         value.PkColumnName = pi.Name;
                     }
                 }
                 EntityPropertyValue propertyValue = new EntityPropertyValue()
                 {
                     ColumnName = name,
                     IsIncrease = false
                 };
                 Attribute isincrease = pi.GetCustomAttribute(typeof(DefaultKeyAttribute));
                 if (isincrease != null)
                 {
                     propertyValue.IsIncrease = true;
                     value.DefaultColumn.Add(name);
                 }
                 valuePairs.Add(pi.Name, propertyValue);
             }
             value.ColoumNamePairs = valuePairs;
             ValuePairs.Add(t, value);
         }
     }
 }