예제 #1
0
        /// <summary>
        /// Finds an item by primary key.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static ActiveRecordBase Find(Type type, object id)
        {
            CheckTypeAtrribute(type);

            try
            {
                PrimaryKeyAttribute pk = ActiveRecordMaster.GetPrimaryKey(type);

                if (pk == null)
                {
                    return(null);
                }

                Criteria criteria = Criteria.For(type);
                criteria.SQLString = ActiveRecordMaster.MakePrimaryKeySelect(type);

                criteria.AddParameter("@CODE", pk.CurrentPropertyInfo.Name, id);

                List <ActiveRecordBase> list = ActiveRecordMaster.ExecuteQueryCriteria(type, criteria);

                if (list != null && list.Count > 0)
                {
                    return(list[0]);
                }

                return(null);
            }
            catch (Exception ex)
            {
                LogManager.Log("Monty.ActiveRecord", LogCategory.Error, String.Format("Error when tries to execute ActiveRecordBase.FindAll(type: [{0}])", type), ex);
                return(null);
            }
        }