/// <summary> /// 根据ID删除记录 /// </summary> /// <param name="id">要删除的记录ID</param> /// <returns></returns> public int DeleteById(object id) { int ret = -1; DeleteCondition con = new DeleteCondition(EntityInfo.DBInfo); con.Tables.Append(EntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(EntityInfo.TableName)); ParamList list = new ParamList(); ScopeList lstScope = new ScopeList(); PrimaryKeyInfo pkInfo = id as PrimaryKeyInfo; if (pkInfo == null) { lstScope.AddEqual(EntityInfo.PrimaryProperty[0].PropertyName, id); } else { pkInfo.FillScope(EntityInfo.PrimaryProperty, lstScope, true); } con.Condition.Append("1=1"); con.Condition.Append(DataAccessCommon.FillCondition(EntityInfo, list, lstScope)); Dictionary <string, bool> cacheTables = null; cacheTables = _oper.DBInfo.QueryCache.CreateMap(EntityInfo.TableName); ret = ExecuteCommand(con.GetSql(true), list, CommandType.Text, cacheTables); return(ret); }
/// <summary> /// 根据主键查找实体 /// </summary> /// <param name="id">主键</param> /// <returns></returns> public T GetEntityById(object id) { ScopeList lstScope = new ScopeList(); PrimaryKeyInfo info = id as PrimaryKeyInfo; if (info == null) { lstScope.AddEqual(CurEntityInfo.PrimaryProperty[0].PropertyName, id); } else { info.FillScope(CurEntityInfo.PrimaryProperty, lstScope, true); } return(GetUnique(lstScope)); }
/// <summary> /// 根据ID获取记录 /// </summary> /// <param name="id">ID</param> /// <param name="isSearchByCache">是否缓存搜索</param> /// <returns></returns> public T GetObjectById(object id, bool isSearchByCache) { ParamList list = null; T ret = default(T); list = new ParamList(); string tabName = CurEntityInfo.DBInfo.CurrentDbAdapter.FormatTableName(CurEntityInfo.TableName); ScopeList lstScope = new ScopeList(); lstScope.UseCache = isSearchByCache; PrimaryKeyInfo pkInfo = id as PrimaryKeyInfo; if (pkInfo == null) { lstScope.AddEqual(CurEntityInfo.PrimaryProperty[0].PropertyName, id); } else { pkInfo.FillScope(CurEntityInfo.PrimaryProperty, lstScope, true); } SelectCondition sc = GetSelectContant(list, lstScope, GetSelectParams(lstScope)); //sql.Append( DataAccessCommon.FillCondition(CurEntityInfo,list, lstScope)); Dictionary <string, bool> cacheTables = null; if (lstScope.UseCache) { cacheTables = _oper.DBInfo.QueryCache.CreateMap(CurEntityInfo.TableName); } using (IDataReader reader = _oper.Query(sc.GetSql(lstScope.UseCache), list, cacheTables)) { if (reader.Read()) { ret = LoadFromReader(reader); } } return(ret); }