(Amber Ferguson,Jorge Ribas,Dalton Bennett/The Washington Post)

The polarizing presidency of Donald Trump shook both parties in the Virginia gubernatorial primary Tuesday and ensured that the “Trump effect” in politics will be a dominant theme in the race in November.

Hostility to Trump spurred strong turnout among Democrats, raising their hopes that primary winner Lt. Gov. Ralph Northam can retain the governor’s mansion for the party.

On the GOP side, enthusiasm for the president lifted outspoken Trump supporter Corey Stewart to an unexpectedly strong finish in his race against the GOP establishment’s favored candidate, Ed Gillespie.

Although Stewart came up short, his showing in the primary creates a new challenge for Gillespie in the general election. Gillespie had hoped to keep some distance from Trump to help him with Virginia’s notably centrist voters. But now he may need to warm up to the president to bring along the Republican base.

Stewart, the Prince William County Board chairman, did dramatically better than polls had projected and underlined the depth of support for Trump in the GOP. He wouldn’t concede and said he wouldn’t help Gillespie in the fall, making Gillespie’s road more uncertain.

“We’ve been backing down too long,” Stewart told cheering supporters Tuesday night. “We’ve been backing down too long in defense of our culture, and our heritage and our country.”

With passionate Democrats eager to send a message of opposition to Trump, and many Republicans determined to defend him, the results pointed to a Virginia campaign certain to attract national attention and funding as a major, early electoral test of the president’s standing.

For Democrats, a major takeaway was the surge of voters energized by outrage over what they described in interviews as Trump’s shortcomings. More than 536,000 people cast ballots in the Democratic primary compared to about 314,000 in the GOP race. Virginia has an open primary, which means that voters do not vote by party affiliation, but they can only request one party’s ballot.

Many Democrats said they would have been satisfied with either Northam or the man he defeated, former U.S. Rep. Tom Perriello, as nominee for governor. The critical thing, they said, was winning in November to register their dissatisfaction with Trump.

Fairfax middle school teacher Tiffany Swanson was one of several who said at the polls she couldn’t remember the last time she voted in a primary election. But she made a point of doing so Tuesday because of Trump.

“Since November, I’ve become much more aware of how these [elections] matter,” said Swanson, 46, who voted for Perriello. “I’m very interested in using my voice more.”

Asked what she disliked about Trump, Swanson said, “the ethical bankruptcy, the flouting of precedent and the law, xenophobia, racism, health care, women’s rights — where do you want me to stop?”

Melinda Silva, 25, of Arlington, said Democrats came out Tuesday “because of what’s happening in the country. It’s pretty disconcerting to see how each week there’s a [congressional] hearing” into Russia’s meddling in the U.S. election, Silva said. Shortly before voting, she had been listening to the congressional testimony by Attorney General Jeff Sessions.

In addition to the symbolic importance of winning the first major gubernatorial election since Trump assumed the presidency, Democrats said it was important that the government in Richmond serve as a bulwark against the president’s policies.

“It’s critical that the states be on the front lines against the Trump agenda,” said Jason Rylander, 45, an attorney in Arlington. He listed his top issues as climate change, health care, education and reproductive rights.

For Republicans, Gillespie performed far worse than anticipated in polls, which had consistently given him a double-digit lead over Stewart and a third candidate, state Sen. Frank Wagner. His final margin over Stewart was about 1.25 percentage points.

Gillespie’s strategy of keeping some distance between himself and Trump — partly to improve his chances against a Democratic opponent in November — appeared to have backfired.

By contrast, Stewart outperformed expectations by pursuing the same brash, aggressive style as Trump. He went out of his way to challenge “political correctness” by defending preservation of Confederate monuments.

Some Republicans said they opted for Stewart as a way to show their distaste for more traditional Virginia Republicans.

“Gillespie is part of the establishment, and we’re trying to get rid of it, like we’re trying to do with Trump,” said Joe Thornlow, 64, a retiree who lives in Gainesville in Prince William County.

A risk for Republicans in backing Stewart was the danger that he would fare worse against the Democrat than the more tempered Gillespie. Hillary Clinton beat Trump in November in Virginia.

Lee Hughes, 70, an auditor in Gainesville, voted for Gillespie partly because he thought the former Republican national chairman had a better chance of winning in November than Stewart or Wagner.

“The only one that can win is Gillespie, in that group,” Hughes said.

Hughes forgave Gillespie for his lukewarm embrace of Trump, saying, “Not everybody has to agree on everything.”

Other Republicans may want their nominee to show more robust support for the president.

“We need people to be supportive of the president to get things done,” said Debra Fleurat, 66, a retired nurse in Gainesville. Asked why she supported Trump, she mentioned his immigration policy and opposition to Muslim sharia law.

On the Democratic side, Northam helped bolster his anti-Trump credentials with a television ad in which he called the president a “narcissistic maniac.”

“I liked the moxie from Northam, who had the nerve say that out loud,” said Gail Ruf, 61, a retiree in Annandale.

Greeting voters at the polls in Gainesville, Gene Rossi, who lost his bid for the Democratic nomination for lieutenant governor, said the anti-Trump feeling was a huge boon for his party.

“Every county meeting I’ve been to has been overflowing with new voters,” Rossi said, adding that Trump was responsible for “95 percent” of the change.

“In the past week, I’ve hardly mentioned Donald Trump, because you don’t need to,” he said. “Donald Trump has a been a gift to the Virginia Democratic Party.”