Herein, taking microRNA-138 (miRNA-138) as a model due to its important role in pre-diagnosis and therapy of Alzheimer's disease, a highly sensitive and selective photoelectrochemical (PEC) sensor was developed based on the magnetic electron donor–acceptor (D–A) covalent organic framework (COF), Fe3O4 @D–A COF, as the photoactive material and ZnSe quantum dots (QDs) as the photocurrent-polarity-switching factor. Fe3O4 @D–A COF, which contained the structure of D–A with a fast separation and transportation of photogenerated carriers, showed a large cathodic photocurrent. ZnSe QDs, as a photosensitive material, matched with D–A COF in energy level and could change the photocurrent polarity of D–A COF. Finally, the obtained Fe3O4 @D–A COF-hairpin DNA1/ZnSe QDs-hairpin DNA2 complex, which was formed through the miR-138-induced catalytic hairpin assembly (CHA) reaction, was adsorbed magnetically on the surface of the magnetic indium tin oxide electrode, producing a large anodic photocurrent. Based on the CHA reaction amplification, magnetic separation, D–A structure of COF and photocurrent-polarity-switching strategy, miRNA-138 was selectively and sensitively assayed (linear range, 1 fM to 10 nM; detection limit, 45 aM). The proposed sensor shows its great prospects in clinical analysis and early diagnosis of disease.