/// <summary> /// 将DataRow转换为 实体类 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dr"></param> /// <returns></returns> T RowToModel <T>(DataRow dr) where T : new() { T model = new T(); foreach (PropertyInfo pi in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance)) { string fieldName = GetDbObjName(pi.Name); if (dr.Table.Columns.Contains(fieldName)) { if (!CommOp.IsEmpty(dr[fieldName])) { pi.SetValue(model, CommOp.HackType(dr[fieldName], pi.PropertyType), null); } } } return(model); }
//在新增记录时,对于单一主键的记录,检查主键字段是否自增或是guid,是否值为空, //如果是自增,则去掉传过来的值,由库自动生成,如果是guid并且为空,则生成一个guid private void CheckPrimaryKey(TableMeta tableMeta, IDictionary <string, object> psin, out string id, out string getMaxIdSql) { id = null; getMaxIdSql = null; var pKeyField = tableMeta.PrimaryKey; if (pKeyField == null) { return; } if (pKeyField.DataType == "Number") { getMaxIdSql = $"select max({pKeyField.FixDbName}) from {tableMeta.FixDbName}"; } var pkey = psin.Keys.FirstOrDefault(k => k.Equals(pKeyField.Name, StringComparison.OrdinalIgnoreCase)); //没有传主键字段过来 if (pkey == null) { pkey = pKeyField.Name; psin.Add(pkey, null); } //当主键为Number型时,认为是自增字段,为空时从参数中去掉,让数据库自动生成 if (pKeyField.DataType == "Number") { psin.Remove(pkey); } //非number型,并且为空,则认为是32位的guid字符串,为它自动生成 else if (CommOp.IsEmpty(psin[pkey])) { id = CommOp.NewId(); psin[pkey] = id; } else { id = CommOp.ToStr(psin[pkey]); } }
/// <summary> /// 生成GET的Action的URL规则 /// </summary> /// <param name="child"></param> /// <param name="info"></param> private void MakeRegTail(AppFunction child, MvcFuncInfo info) { ParameterInfo pi = info.Parameters .FirstOrDefault(p => p.Name.Equals("id", StringComparison.OrdinalIgnoreCase)); if (pi == null) { return; } if (!pi.ParameterType.Name.Contains("Int")) { return; } else if (!CommOp.IsEmpty(pi.DefaultValue)) { child.LocationSamples = child.Location + Environment.NewLine + child.Location + "/123"; child.RegTail = "(/\\d*)?"; } else { child.LocationSamples = child.Location + "/123"; child.RegTail = "/\\d+"; } }