public object Load(IDataMapper db, object parent) { TParent tParent = (TParent)parent; if (Condition != null && !Condition(tParent)) { return(null); } else { if (_relationshipType == RelationshipTypes.One) { // Parent FK = Child PK if (_childColumns.PrimaryKeys.Count != 1) { throw new DataMappingException(string.Format("'{0}' must have exactly one primary key mapped.", typeof(TChild).Name)); } var childPK = _childColumns.PrimaryKeys[0]; var parentFK = _parentColumns.GetByFieldName(_fk); if (parentFK == null) { throw new DataMappingException(string.Format("'{0}' does not contain foreign key field '{1}'.", typeof(TParent), _fk)); } db.AddParameter("@FK", parentFK.Getter(parent)); var query = db.Query <TChild>(); string whereClause = query.BuildColumnName(childPK.ColumnInfo.Name) + "=@FK"; return(query.Where(whereClause).FirstOrDefault()); } else if (_relationshipType == RelationshipTypes.Many) { // Parent PK = Child FK if (_parentColumns.PrimaryKeys.Count != 1) { throw new DataMappingException(string.Format("'{0}' must have exactly one primary key mapped.", typeof(TParent).Name)); } var parentPK = _parentColumns.PrimaryKeys[0]; var childFK = _childColumns.GetByFieldName(_fk); if (childFK == null) { throw new DataMappingException(string.Format("'{0}' does not contain foreign key field '{1}'.", typeof(TChild), _fk)); } db.AddParameter("@PK", parentPK.Getter(parent)); var query = db.Query <TChild>(); string whereClause = query.BuildColumnName(childFK.ColumnInfo.Name) + "=@PK"; return(query.Where(whereClause).ToList()); } else { // This should not happen because relationship type should be inferred already throw new DataMappingException(string.Format("Unable to infer relationship type for parent '{0}' -> '{1}'.", typeof(TParent).Name, typeof(TChild).Name)); } } }
public virtual InsertQueryBuilder <T> ColumnsIncluding(params string[] properties) { _columnsToInsert = new ColumnMapCollection(); foreach (string propertyName in properties) { _columnsToInsert.Add(_mappings.GetByFieldName(propertyName)); } return(this); }