KANSAS CITY, Kan. — Sharice Davids, a leading Democrat in a key congressional primary election on Tuesday, finished a White House fellowship in the early months of the Trump administration. As a lesbian and a Native American, she became convinced that hard-won progress on issues like gay rights and the environment would erode under Mr. Trump, and thought Kansans in her district might support her as a counterforce to the president.

​“We had to focus on getting more people elected to decision-making positions because that’s the way that we offset someone who wants to destroy the E.P.A. being appointed to run the E.P.A,” she said, referring to Scott Pruitt, Mr. Trump’s now-departed agency administrator.

Ms. Davids is among more than 400 gay, lesbian, bisexual or transgender candidates running for office this year — a record number, according to groups that track such data. Most are Democrats, and several are mounting anti-Trump congressional bids with a message broader than gay rights. Ms. Davids says she talks mostly about issues like health care and only had one exchange with a voter who questioned whether a gay person could win.

Around half of these candidates are running for state offices, a priority for activists who say many of the most important civil rights battles are happening close to home. In 2017 , more than 120 bills described as “anti-L.G.B.T.” were introduced across 30 states, including adoption laws and so-called bathroom bills , according to the Human Rights Campaign. By January , 12 of them had become law.