/// <summary> /// 快速获得反射信息 /// </summary> public override void InternalInit() { if (this.OrmHost != null) { String tblName = OrmHost.GetTableName(this.GetType(), this); EntityInfo eif = OrmHost.TableManager[tblName]; this.pifs = eif.pifs; this.PropertyNameSet = eif.PropertyNames; this.PrimaryKeyPropertyName = eif.PrimaryKeyPropertyName; this.PrimaryKeyPropertyQuote = eif.PrimaryKeyPropertyQuote; this.PrimaryPropertyInfo = eif.PrimaryPropertyInfo; } }
/// <summary> /// 更新一条记录,注意请保持SaveMode为SaveMode.UPDATE /// </summary> /// <param name="mode"></param> /// <param name="TableName"></param> /// <param name="args"></param> public override void Save(EntitySaveMode mode = EntitySaveMode.UPDATE, String TableName = null) { try { SimplifiedEntity entity = this; String tableName = null; Type EntityType = null; PropertyInfo[] pifs; StringBuilder sb; EntityType = entity.GetType(); if (TableName == null) { String __tableName = OrmHost.GetTableName(this.HostEntityType, null); tableName = __tableName; } else { tableName = TableName; if (tableName != null && tableName.EndsWith("Entity") && tableName != "Entity") { if (tableName.EndsWith("_Entity")) { tableName = tableName.Remove(tableName.Length - "_Entity".Length, "_Entity".Length); } if (tableName.EndsWith("Entity")) { tableName = tableName.Remove(tableName.Length - "Entity".Length, "Entity".Length); } } } pifs = entity.pifs; sb = new StringBuilder(); sb.Clear(); foreach (var item in pifs) { if (item.Name.Equals("Id")) { continue; } DBKeyAttribute attr = item.GetCustomAttribute <DBKeyAttribute>(); if (attr != null && attr.KeyDescription.Equals(DBKeyAttribute.PRIMARYKEY)) { continue; } Object obj = item.GetValue(entity, null); String value = obj?.ToString(); if (value != null && value.Contains("'")) { value = value.Replace("\'", "\'\'"); } value = GetEntityPropertyValue(item.PropertyType, obj); sb.Append( String.Format("{0}={1},", item.Name, value) ); } String sql = sb.ToString().TrimEnd(','); OrmHost.AdonetContext.Update(tableName, sql + String.Format(" where {0}={1}", entity.PrimaryKeyPropertyName, entity.GetPrimaryKeyValue()) ); } catch (Exception e) { OrmHost?.AdonetContext.PerformErrorHandler(this, e); } }