Lawmakers in the state Senate engaged with Mayor Bill de Blasio for nearly four hours on Wednesday on the renewal of mayoral control of New York City schools.

For de Blasio, the appearance in Albany came with pitfalls: He’d be testifying before Senate Republicans, a conference that has held deep antipathy for him and his policies, which has only increased in the wake of an investigation into his fundraising efforts on behalf of Democratic candidates in 2014.

In the end, however, the mayor’s testimony and exchanges with lawmakers from both parties was largely cordial and amounted to a lengthy discussion on education policy issues facing New York City.

The only actual fireworks came when de Blasio was challenged by Republican Sen. Terrence Murphy, who has been a staunch critic of the mayor and is a freshman lawmaker from a suburban Senate district that includes Putnam and Westchester counties.

“Convince me,” Murphy said, “with all the allegations going on in your office why I should vote for mayoral control.”

De Blasio responded by pointing to successes under the mayoral control program, including higher graduation rates and test scores as well as an expansion of computer science courses.

“These are major changes in the way we approach education,” de Blasio said. “It’s only possible under a mayoral control system.”

Murphy, however, wasn’t necessarily convinced.

“There’s one thing you forgot and that’s the trust factor,” Murphy said. “You have to have the trust of the people.”

The mayor shot back: “The public trusts the actual positive changes in their lives.”

And he added the allegations that have been raised shouldn’t count against the mayoral control policy itself or his administration.

“In our democracy, we don’t judge by allegations,” he said, “we judge by facts.”

Murphy’s political allies had initially complained over the Democratic fundraising tactics in 2014, when donors at de Blasio’s urging gave heavily to county party committees, which in turn supported candidates running in key battleground races.

Murphy succeeded in the Senate Republican Greg Ball, a maverick GOP lawmaker and conservative firebrand who moved to Texas rather than seek re-election.

De Blasio’s political team had backed Democrat Justin Wagner to replace Ball, one of a half-dozen competitive races that year.

Mayoral control is due to expire in June after de Blasio received a 12-month extension last year.