It was Harry Potter Night at Hadlock Field, so it’s apropos to talk about the Portland Sea Dogs needing a magical ending Tuesday after the Trenton Thunder scored seven runs in the second inning.

It almost happened.

Despite falling behind 9-1 early, the Sea Dogs had the tying run at the plate in the ninth inning before losing 9-6 to the Trenton Thunder.

Trenton, the minor league affiliate of the team-that-shall-not-be-named, improved to 63-31, easily the best record in the Eastern League, 12 wins better than anyone else. The Sea Dogs dropped to 43-49.

Portland put together 13 hits, giving the Hadlock crowd something to cheer about – while answering Harry Potter trivia questions between innings.

Deiner Lopez, Jeremy Barfield, Mike Olt and Josh Tobias all had two hits for Portland. Barfield drove in two runs. Michael Chavis, Tobias and Nick Lovullo each had RBI singles.

“I like the way we battled,” Sea Dogs Manager Carlos Febles said. “It’s tough to come back from 9-1, but we gave ourselves a chance.”

With starter Kevin McAvoy recently placed on the disabled list, Portland used reliever Jake Drehoff (1-1) in the spot start. Drehoff gave up the seven-run second and, the crazy thing is, he almost escaped it untouched.

Drehoff, a 13th-round draft pick in 2013 out of the University of Southern Mississippi, got into a first-inning jam, with runners on second and third with one out. But he responded with two strikeouts.

In the second inning, two singles and a walk loaded the bases with no outs. Drehoff retired the next two batters on a pop-up to second and a shallow lineout to right.

But Tito Polo lined a single to right, scoring a run. Two RBI singles followed for a 5-0 lead, and Zach Zehner completed the damage with a two-run homer over the left-field wall.

“He almost got out of it,” Febles said. “He left some pitches up.”

The Sea Dogs, who have had four pitchers promoted to Triple-A – including Ty Buttrey who was sent back to Pawtucket Tuesday – have the worst ERA in the Eastern League (4.71).

But the bullpen came through Tuesday. Jacob Dahlstrand, just off the disabled list, pitched two scoreless innings, before getting nicked for two runs in the fifth. Both Jake Cosart and Luis Ysla pitched two shutout innings.

In the ninth, with Portland down 9-4, Danny Mars drew a one-out walk, Lopez singled and Chavis singled in Mars to close to 9-5. Trenton brought in closer Cale Coshow and Barfield greeted him with a double to left, scoring Lopez.

With runners on second and third, Coshow got Olt to pop up and then struck out Cole Sturgeon for the win, and his 13th save.

NOTES: The announced paid attendance was 5,864 …

Mars was 1 for 3 with two walks to up his on-base percentage to .359 …

In Triple-A, Rafael Devers went 2 for 4 and is batting .467 in four games with Pawtucket. Bryce Brentz hit his 20th home run. Buttrey pitched three scoreless innings for the win …

Wednesday’s game at Hadlock is a noon start.