ALEXANDRIA, Va. — For Ed Gillespie, Trumpism was an ill-fitting suit.

His résumé was pure establishment — national Republican Party chairman, counselor to President George W. Bush, well-connected K Street lobbyist. But the messaging of his campaign for governor of Virginia was that of a cultural flamethrower, emphasizing crimes by undocumented immigrants as well as monuments to Confederate heroes — and even suggesting that his opponent, a pediatric neurologist, supported child pornographers.

As the Republican candidate, Mr. Gillespie tried to run in a very narrow lane by embracing some of the most divisive elements of President Trump’s agenda while treating him like Voldemort and mostly refusing to utter his name. It was enough to motivate Mr. Trump’s supporters in rural parts of the state, but fell far short in Northern Virginia, where the wealthy and well-educated voters who were once reliably Republican continued their march toward becoming solidly Democratic.

Lessons from off-year elections can be overdrawn, but the Virginia race strongly suggests that Republicans running in swing states will have to choose a side rather than try to straddle an uncomfortable line. Mr. Trump’s blunt force, all-or-nothing approach has worked in deeply conservative areas, but Republicans will have trouble replicating that in certain states in the midterms next year when faced with a diverse, highly educated electorate like the one in Virginia.