public T GetByID <T>(object id) where T : new() { SchemaTable t = Schema.EnsureSchema(typeof(T)); if (t != null) { ORMSelect s = new ORMSelect(Adp); s.FocalType = typeof(T); s.Condition = new ORMCond(t.GetPrimaryKeyColumn().Property.Name, SQRelationOperators.Equal, id); s.Top = 1; List <T> result = Select <T>(s); if (result.Count > 0) { return(result[0]); } } return(default(T)); }
public virtual void Insert(object o) { if (o != null) { SchemaTable t = Schema.EnsureSchema(o.GetType()); if (t != null) { SQInsertQuery q = new SQInsertQuery() { Table = new SQAliasableObject(t.Table.Name), ReturnID = t.GetPrimaryKeyColumn() != null && t.GetPrimaryKeyColumn().Column.IsIdentity }; PopulateSetQuery(q, o, t); SQSelectResult sr = Adp.Insert(q); try { if (q.ReturnID) { if (sr.Reader.Read()) { SchemaColumn pkCol = t.GetPrimaryKeyColumn(); object id = sr.Reader.GetValue(0); if (id.GetType() != pkCol.Property.PropertyType) { switch (pkCol.Column.DataType) { case SQDataTypes.Int16: id = Convert.ToInt16(id); break; case SQDataTypes.Int32: id = Convert.ToInt32(id); break; case SQDataTypes.Int64: id = Convert.ToInt64(id); break; case SQDataTypes.Decimal: id = Convert.ToDecimal(id); break; } } if (o is IPopulateProperties) { ((IPopulateProperties)o).PopulateProperty(pkCol.Property.Name, id); } else { pkCol.Property.SetValue(o, id, null); } } } } finally { sr.Close(); } } else { throw new InvalidTypeException(o.GetType()); } } }