President Donald Trump talks with Tesla and SpaceX CEO Elon Musk at Trump Tower last February.

Even some of the tech industry's most prominent critics of President Donald Trump are opening their checkbooks and donating to Republican lawmakers, as Silicon Valley sets its sights on the 2018 midterm election.

With the entire House on next year's ballot — and about one-third of the U.S. Senate up for a vote, too — the stakes are high for those in the Bay Area who seek to erode the GOP's control of Congress and erect a new bulwark against Trump's agenda in areas like immigration and climate change.

But some of the region's most politically active executives — including Eric Schmidt, the executive chairman of Google's parent company, Alphabet; and Elon Musk, the founder of Tesla and SpaceX — have contributed generously to Republicans in recent months in a bid to maintain steady relationships with both parties.

At the end of March, for example, Musk chipped in $50,000 to an organization run by House Majority Leader Kevin McCarthy that's meant to boost his GOP allies. Musk also donated roughly $34,000 to the Republican Party's official arm for electing lawmakers to the chamber, federal records show.

Yet Musk's support comes despite his public rift with the president.

Initially, the SpaceX and Tesla founder tried to maintain a positive working relationship with Trump, even serving as an adviser to the White House on economic issues. But Musk severed those ties in June, citing Trump's decision to withdraw the United States from the Paris climate agreement — a pact to reduce carbon emissions around the world that Musk and other business leaders had supported. A spokesman for Musk did not immediately respond to emailed questions on Thursday.

Nevertheless, Musk's early 2018 donations affirm a well-worn, if uncomfortable, political reality in Silicon Valley. For all of the region's protests of the president, tech companies must engage both parties in equal measure if they have any hope of shaping the government's debates over tax and immigration reforms — or the myriad other issues that matter to the tech industry's bottom lines.

"These are all things right now the administration is talking about, that Republicans are talking about ... even if you don't like the other issues," said Aaron Ginn, a co-founder of the Lincoln Network, a Bay Area group that tries to connect GOP lawmakers to tech.

With congressional races, the likes of Amazon, Facebook and Google always have sought to apply an even political hand. In the 2016 election, for example, these and other internet giants donated about $1.8 million to Republican campaigns, compared to $1.5 million on Democratic candidates, according to the Center for Responsive Politics.

Some in the tech industry lambasted Trump as a candidate, others withdrew from the GOP convention supporting him, and still more held fundraisers for his ill-fated Democratic opponent, Hillary Clinton. But they all simultaneously sought to improve their relationships with Republicans on Capitol Hill, believing at the time — correctly — that they would retain control of Congress.

In 2018, however, the midterm elections serve as a referendum on Trump's agenda. Lacking the support of friendly federal lawmakers, the president won't be able to deliver on his promises to lower corporate taxes, overhaul immigration laws, invest in U.S. infrastructure and strike new trade deals with other countries — some more problematic to the tech industry than others.

Trump may already have broad executive powers to institute measures like a travel ban, and his administration long ago set out to unwind decades of federal regulations across government. But his other priorities — like efforts to scrap Obamacare, a concern to many in liberal-leaning Silicon Valley — depend nonetheless on Capitol Hill.

There, GOP lawmakers got a boost earlier this year from Google's Schmidt, who donated $33,900 — the maximum contribution — to the National Republican Senatorial Committee, according to FEC records. Google's chief executive, Sundar Pichai, also wrote a $33,900 to the NRSC, federal disclosures show, but he contributed the same amount to a similar fund designed to boost Senate Democrats.

Schmidt has served as Google's primary emissary to the White House, most recently attending a June summit with the president and other tech leaders. Behind the scenes, though, he has fretted over the "evil" things Trump might do in areas like immigration, while Pichai has publicly criticized the White House for its executive orders targeting majority-Muslim countries. A spokeswoman for Google declined to comment.

Other early Republican donors in the tech industry include Safra Catz, the CEO of Oracle and an adviser to Trump, as well as some big names in the venture-capital community, like Keith Rabois at Khosla Ventures and Ted Ullyot at Andreessen Horowitz. All three contributed the maximum amount to the NRSC.

Meanwhile, some of the earliest and most active Democratic donors include Sheryl Sandberg, the chief operating officer of Facebook.

In April, Sandberg wrote a $25,000 check for an organization run by Emily's List that helps elect pro-choice Democratic women to the U.S. Congress. Since the beginning of 2017, she has also contributed thousands of dollars toward the re-election of Missouri Sen. Claire McCaskill, New Mexico Sen. Martin Heinrich, New York Sen. Kirsten Gillibrand and California Rep. Zoe Lofgren, all Democrats.

Palantir CEO Alex Karp, for his part, has donated to Heinrich and McCaskill as well. He had been a major Clinton donor in 2016, though he's benefitted during the Trump administration given the president's relationship with Peter Thiel, an early investor in Karp's company.