EntityInfo GetEntityInfo(PropertyInfo[] pifs) { EntityInfo entityInfo = new EntityInfo(); entityInfo.pifs = pifs; foreach (var item in pifs) { entityInfo.PropertyNames.Add(item.Name); DBKeyAttribute attr = item.GetCustomAttribute <DBKeyAttribute>(); if (attr != null) { if (attr.KeyDescription.Equals(DBKeyAttribute.PRIMARYKEY)) { entityInfo.PrimaryKeyPropertyName = item.Name; entityInfo.PrimaryPropertyInfo = item; String propertyTypeName = item.PropertyType.Name; if (propertyTypeName.Equals("DateTime") || propertyTypeName.Equals("String")) { entityInfo.PrimaryKeyPropertyQuote = true; } else { entityInfo.PrimaryKeyPropertyQuote = false; } } } } return(entityInfo); }
/// <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); } }