/// <summary> /// 获取实体的特性 /// </summary> /// <returns></returns> public static string GetEntityTableAtrributes <TEntity>() where TEntity : class, new() { TEntity model = new TEntity(); object[] attrs = model.GetType().GetCustomAttributes(typeof(TableAttribute), false); if (attrs.Count() > 0) { TableAttribute attr = attrs[0] as TableAttribute; return(attr.Name); } return(model.GetType().Name); }
/// <summary> /// 获取实体的列特性 /// </summary> /// <returns></returns> public string GetEntityTableAtrributes <TEntity>() where TEntity : new() { List <EntityPropColumnAttributes> list = new List <EntityPropColumnAttributes>(); TEntity model = new TEntity(); object[] attrs = model.GetType().GetCustomAttributes(typeof(TableAttribute), false); if (attrs.Count() > 0) { TableAttribute attr = attrs[0] as TableAttribute; return(attr.Name); } return(model.GetType().Name); }
/// <summary> /// 获取实体的列特性 /// </summary> /// <returns></returns> public List <EntityPropColumnAttributes> GetEntityColumnAtrributes <TEntity>() where TEntity : new() { List <EntityPropColumnAttributes> list = new List <EntityPropColumnAttributes>(); TEntity model = new TEntity(); foreach (PropertyInfo prop in model.GetType().GetProperties()) { EntityPropColumnAttributes entity = new EntityPropColumnAttributes(); entity.propName = prop.Name; entity.fieldName = prop.Name; entity.isPrimaryKey = false; entity.isIdentity = false; Type type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType; entity.typeName = type.FullName; object[] CustomAttributesArr = prop.GetCustomAttributes(typeof(ColumnAttribute), false); if (CustomAttributesArr.Count() > 0) { foreach (var obj in CustomAttributesArr) { ColumnAttribute attr = obj as ColumnAttribute; entity.fieldName = attr.Name; entity.isPrimaryKey = attr.IsPrimaryKey; entity.isIdentity = attr.IsDbGenerated; } } } return(list); }
public APIPropertyContext Property(string propertyName) { APIPropertyContext _newPropContext = new APIPropertyContext(ref _current_entity); _newPropContext._thisFieldName = propertyName; _newPropContext.SetField(_current_entity.GetType().GetProperty(propertyName)); _newPropContext.fieldupdater = UpdateField; return(_newPropContext); }
public static string ModelMapperSelectCommand <TEntity>() where TEntity : class, new() { TEntity instance = new TEntity(); PropertyInfo[] propertyInfos = typeof(TEntity).GetProperties (BindingFlags.Public | BindingFlags.Instance); string select = "SELECT "; select += PropertiesLoop(propertyInfos, false); select += $" FROM {instance.GetType().Name}"; return(select); }
public List <TEntity> Set <TEntity>() where TEntity : class, new() { var model = new TEntity(); var type = model.GetType(); var props = type.GetProperties(); var itemCodes = string.Join(",", props.Select(x => x.Name)); var tableName = GetTableName(type); var executeSql = $"SELECT {itemCodes} FROM {tableName} "; var table = GetDataTable(executeSql, CommandType.Text); return(table.ToObjectList <TEntity>()); }
/// <summary> /// 查找父表实体 /// </summary> /// <typeparam name="TEntity">父表实体类</typeparam> /// <returns></returns> public DataContext Parent <TEntity>() where TEntity : new() { string error = ""; type = entity.GetType(); object t = new TEntity(); Type ptype = t.GetType(); string table = t.GetType().Name; #region 单个实体 PropertyInfo pi = type.GetProperty(table);//主表中的明细对象的 [属性名称] 一定要跟明细表的 [类名] 一样 if (pi != null) { if (pi.PropertyType.IsClass) { if (pi.PropertyType.Name == table) { //try //{ string foreignkey = entity.GetType().GetProperty("ForeignKey").GetValue(entity, null).ToString(); //外键字段名称 string foreignkeyname = entity.GetType().GetProperty("ForeignKeyName").GetValue(entity, null).ToString(); //从表储存外键值字段名称 try { string value = type.GetProperty(foreignkeyname).GetValue(entity, null).ToString(); //外键字段值 string sql2 = "select * from " + table + " where " + foreignkey + "='" + value + "'"; t = GetChildClass(t, sql2); pi.SetValue(entity, t, null); } catch (Exception e) { error = pi.PropertyType.Name + ":" + e.Message; Result.Error += error + ";"; } } } } #endregion return(this); }
/// <summary> /// 获取泛型的描述 /// </summary> /// <returns></returns> public static List <string> GetTEntityDescriptions <TEntity>() where TEntity : class, new() { TEntity entity = new TEntity(); List <string> list = new List <string>(); object[] obj = entity.GetType().GetCustomAttributes(typeof(DescriptionAttribute), false); foreach (var s in obj) { DescriptionAttribute attr = s as DescriptionAttribute; list.Add(attr.Description); } return(list); }
/// <summary> /// 获取泛型属性的描述 /// </summary> /// <param name="text">内容</param> public static IDictionary <string, string> GetPropertyDescriptions <TEntity>() where TEntity : class, new() { TEntity entity = new TEntity(); IDictionary <string, string> idc = new Dictionary <string, string>(); foreach (PropertyInfo prop in entity.GetType().GetProperties()) { object[] objs = prop.GetCustomAttributes(typeof(DescriptionAttribute), false); string value = ""; if (objs.Length > 0) { DescriptionAttribute description = objs[0] as DescriptionAttribute; value = description.Description; } idc.Add(prop.Name, value); } return(idc); }
/// <summary> /// 获取实体的列特性 /// </summary> /// <returns></returns> public static List <EntityPropColumnAttributes> GetEntityColumnAtrributes <TEntity>() where TEntity : class, new() { List <EntityPropColumnAttributes> list = new List <EntityPropColumnAttributes>(); TEntity model = new TEntity(); foreach (PropertyInfo prop in model.GetType().GetProperties()) { EntityPropColumnAttributes entity = new EntityPropColumnAttributes(); entity.propName = prop.Name; entity.fieldName = prop.Name; entity.isPrimaryKey = false; entity.isDbGenerated = false; Type type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType; entity.typeName = type.FullName; ColumnAttribute columnAttribute = prop.GetCustomAttribute <ColumnAttribute>(); if (columnAttribute != null) { entity.fieldName = columnAttribute.Name; } KeyAttribute keyAttribute = prop.GetCustomAttribute <KeyAttribute>(); if (keyAttribute != null) { entity.isPrimaryKey = true; } DatabaseGeneratedAttribute databaseGeneratedAttribute = prop.GetCustomAttribute <DatabaseGeneratedAttribute>(); if (databaseGeneratedAttribute != null) { entity.isDbGenerated = databaseGeneratedAttribute.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity ? true : false; } list.Add(entity); } return(list); }
private void IncludeList <TEntity>() where TEntity : new() { string error = ""; //type = entity.GetType(); object t = new TEntity(); string table = t.GetType().Name; string datatableObjectName = null; foreach (object en in entityList) { type = en.GetType(); #region 单个实体 PropertyInfo pi = type.GetProperty(table);//主表中的明细对象的 [属性名称] 一定要跟明细表的 [类名] 一样 if (pi != null) { if (pi.PropertyType.IsClass) { if (pi.PropertyType.Name == table) { //try //{ string foreignkey = t.GetType().GetProperty("ForeignKey").GetValue(t, null).ToString(); //外键字段名称 string foreignkeyname = t.GetType().GetProperty("ForeignKeyName").GetValue(t, null).ToString(); //从表储存外键值字段名称 if (type.GetProperty(foreignkey).GetValue(entity, null) != null) { try { string value = type.GetProperty(foreignkey).GetValue(entity, null).ToString();//外键字段值 string sql2 = "select * from " + table + " where " + foreignkeyname + "='" + value + "'"; t = GetChildClass(t, sql2); pi.SetValue(entity, t, null); } catch (Exception e) { error = pi.PropertyType.Name + ":" + e.Message; Result.Error += error + ";"; } } //} //catch (Exception e) //{ // error = e.Message; // Result.Error += error+";"; //} } } } #endregion #region 多个实体 foreach (PropertyInfo p in type.GetProperties()) { if (p.PropertyType.IsClass && p.PropertyType.UnderlyingSystemType.Name == "List`1") { string A = p.ToString();//System.Collections.Generic.List`1[SMT.Test.SMT_TEST_DETAIL] SMT_TEST_DETAIL2 string ptablename = A.Substring(0, A.IndexOf(']')).Substring(A.LastIndexOf('.') + 1); if (table == ptablename) { try { #region List <TEntity> list = new List <TEntity>(); string foreignkey = t.GetType().GetProperty("ForeignKey").GetValue(t, null).ToString(); //外键字段名称 string foreignkeyname = t.GetType().GetProperty("ForeignKeyName").GetValue(t, null).ToString(); //从表储存外键值字段名称 if (type.GetProperty(foreignkey).GetValue(entity, null) != null) { string value = type.GetProperty(foreignkey).GetValue(entity, null).ToString();//外键字段值 string sql2 = "select * from " + table + " where " + foreignkeyname + "='" + value + "'"; DataTable dtt = SMT.DataProvider.GetDataTable(sql2).DataTable; for (int i = 0; i < dtt.Rows.Count; i++) { t = new TEntity(); foreach (PropertyInfo p2 in t.GetType().GetProperties()) { if (dtt.Columns.Contains(p2.Name)) { p2.SetValue(t, SMTProperty.SetPropertyValue(p2, dtt.Rows[i][p2.Name]), null); try { p2.SetValue(t, SMTProperty.SetPropertyValue(p2, dtt.Rows[i][p2.Name]), null); } catch (Exception e) { error = p2.Name + ":" + e.Message; Result.Error += error + ";"; } } } list.Add((TEntity)t); } p.SetValue(entity, list, null); } #endregion } catch (Exception e) { error = e.Message; Result.Error += error + ";"; } } } } #endregion #region DatatTable foreach (PropertyInfo p in type.GetProperties()) { if (p.PropertyType.Name.ToLower() == "datatable") { datatableObjectName = p.Name; if (datatableObjectName.Substring(0, datatableObjectName.LastIndexOf('_')) == table) { PropertyInfo dp = type.GetProperty(datatableObjectName); if (dp.PropertyType.IsClass) { try { string foreignkey = t.GetType().GetProperty("ForeignKey").GetValue(t, null).ToString(); //外键字段名称 string foreignkeyname = t.GetType().GetProperty("ForeignKeyName").GetValue(t, null).ToString(); //从表储存外键值字段名称 if (type.GetProperty(foreignkey).GetValue(entity, null) != null) { try { string value = type.GetProperty(foreignkey).GetValue(entity, null).ToString();//外键字段值 string sqld = "select * from " + table + " where " + foreignkeyname + "='" + value + "'"; dp.SetValue(entity, SMT.DataProvider.GetDataTable(sqld).DataTable, null); } catch (Exception e) { error = p.PropertyType.Name + ":" + e.Message; Result.Error += error + ";"; } } } catch (Exception e) { error = e.Message; Result.Error += error + ";"; } } } } } #endregion } }
public PocoTable(object primaryKeyValue, PocoDatabase db) { // Construct this.Construct(); // Flag this.DbSucces = false; // Check Point if (primaryKeyValue == null) { return; } // Check Point if (string.IsNullOrEmpty(this.TableName) || string.IsNullOrEmpty(this.PrimaryKey)) { return; } // Check Point if (db == null && this.DatabaseOptions == null) { return; } // Check PocoDatabase if (db == null && this.DatabaseOptions != null) { db = new PocoDatabase(this.DatabaseOptions.DatabaseEngine, this.DatabaseOptions.ConnectionString); } // Create Entity TEntity data = null; // Get Data // Bunu ekleyince sadece 1 defa güncelleme yapıyor ve sonra objeyi yokediyor. // using (db) { data = db.GetConnection().Query <TEntity>("SELECT * FROM " + this.TableName + " WHERE " + this.PrimaryKey + "='" + primaryKeyValue + "'", this).FirstOrDefault(); } // Check Data if (data != null) { PropertyInfo[] dataProperties = data.GetType().GetProperties(); PropertyInfo[] pocoProperties = this.GetType().GetProperties(); foreach (PropertyInfo pocoProperty in pocoProperties) { // Check Point if (pocoProperty.Name == "Item") { continue; } // Find Correct Property and Set foreach (PropertyInfo dataProperty in dataProperties) { if (dataProperty.Name == pocoProperty.Name) { // Set Value pocoProperty.SetValue(this, dataProperty.GetValue(data)); // Break break; } } } // Flag this.DbSucces = true; } }
public APIPropertyContext(ref TEntity CurrentEntity) { _CurrentEntity = CurrentEntity; thisType = CurrentEntity.GetType(); isModified = false; }
public APIPropertyGroup(ref TEntity CurrentEntity) { _CurrentEntity = CurrentEntity; thisType = _CurrentEntity.GetType(); }