コード例 #1
0
 public T GetByKeys <T>(T entity) where T : new()
 {
     PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
     ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);
     try
     {
         ORMHelper.CheckEntityKey(entity);
         SelectCommandCreator dbCommandCreator = new SelectCommandCreator(dbAccess);
         dbCommandCreator.Entity     = entity;
         dbCommandCreator.SelectType = SelectType.GetOneByEntityKey;
         DbCommand dbCommand = dbCommandCreator.GetDbCommand();
         DataTable dt        = dbAccess.GetDataTableByCommand(dbCommand);
         if (dt.Rows.Count == 0)
         {
             return(default(T));
         }
         else if (dt.Rows.Count == 1)
         {
             return(ORMHelper.ConvertDataRowToEntity <T>(dt.Rows[0]));
         }
         else
         {
             throw new ORMException(ErrorMessages.ResultNotUniqueMessage);
         }
     }
     catch (Exception ex)
     {
         throw new ORMException("Get object by keys error, keys value:\n\t" + ORMHelper.GetEntityInfoMessage(entity), ex);
     }
 }