public virtual IDbCommand GetInsertCommand(ModelBase model) { //preparing the update query StringBuilder sb = new StringBuilder(), sb_value = new StringBuilder(); sb.AppendFormat("INSERT INTO {0} ( ", model.GetTableName()); sb_value.Append(" VALUES("); IDbCommand cmd = GetCommand(); PropertyInfo prop = null; List <PropertyInfo> FieldList = model.GetFieldList(); PropertyInfo keyField = model.GetKeyPropertyInfo(); for (int i = 0, len = FieldList.Count; i < len; i++) { prop = FieldList[i]; if (model.DmlIgnoreFields.Contains(prop.Name)) { continue; } if (FieldList[i].GetCustomAttribute(typeof(NotMappedAttribute)) != null) { continue; } if (prop.Name == keyField.Name) { if (model.GetSequenceName() != null) { //sb.AppendFormat(" {0},", prop.Name); //sb_value.AppendFormat(" {0}.NEXTVAL,", model.GetSequenceName()); continue; } } else { if (prop.GetValue(model) == null) { //db default will be saved. continue; } else { //if (prop.Name == "date_ad" || prop.Name == "date_bs" || prop.Name == "dob_ad") if (prop.PropertyType == typeof(DateTime) || prop.PropertyType == typeof(DateTime?)) { if (prop.GetCustomAttributes(typeof(CurrentTimestampAttribute), true).Length > 0) { sb.AppendFormat(" {0},", prop.Name); //sb_value.AppendFormat(" {0},", Connection.TimeStampText); } else { sb.AppendFormat(" {0},", prop.Name); //sb_value.AppendFormat(" {0},", prop.Name, Connection.GetDBDate(prop.GetValue(model))); } } else { sb.AppendFormat(" {0},", prop.Name); sb_value.AppendFormat(" {1}{0},", prop.Name, ParamPrefix); //sb_value.AppendFormat(" trim({1}{0}),", prop.Name, ParamPrefix); cmd.Parameters.Add(CreateParameter(prop, model)); } } } } if (sb.Length > 0) { sb.Remove(sb.Length - 1, 1); sb.Append(")"); } if (sb_value.Length > 0) { sb_value.Remove(sb_value.Length - 1, 1); sb_value.Append(") "); sb.Append(sb_value.ToString()); } //TODO: this code is Postgre specific so need to be changed in future to make way for other database // This line return the primary key value which is recently added sb.AppendFormat(" RETURNING {0}", model.GetAllFields(string.Empty, false, false)); cmd.CommandText = sb.ToString(); return(cmd); }