Ejemplo n.º 1
0
 public void AddColum(String name, EntityPropertyValue value)
 {
     if (ColoumNamePairs == null)
     {
         ColoumNamePairs = new Dictionary <String, EntityPropertyValue>();
     }
     ColoumNamePairs.Add(name, value);
 }
Ejemplo 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(IncreaseKeyAttribute));
                 if (isincrease != null)
                 {
                     propertyValue.IsIncrease = true;
                     value.IncresementColumn.Add(name);
                 }
                 valuePairs.Add(pi.Name, propertyValue);
             }
             value.ColoumNamePairs = valuePairs;
             ValuePairs.Add(t, value);
         }
     }
 }