The Virginia Senate approved a bill Friday that would prevent the state government from discriminating based on sexual orientation, a key milestone in a debate that has lurched back and forth repeatedly in recent years.

The measure passed on a 24 to 16 vote, with four Republicans joining all of the chamber’s Democrats in favor. A companion measure has not been offered in the House; supporters were focused on getting it through the Senate first.

“It is about fairness,” said Sen. Donald A. McEachin (D-Henrico), the bill’s lead sponsor. “It is about making sure no state employee worries about being discriminated against.”

Sen. Adam Ebbin (D-Alexandria), an openly gay lawmaker who has worked on the issue since 2000, before he was in elected office, said backers would “make the case that Virginia needs to be competitive with the private sector for the best and brightest employees.”

Ebbin and his allies hope to enshrine in Virginia law a policy that has seesawed depending on who occupies the governor’s mansion. Democratic Govs. Mark Warner and Timothy M. Kaine both signed executive orders prohibiting discrimination based on sexual orientation, but Gov. Robert McDonnell (R) chose not to.

McDonnell faced pressure on the issue in 2010 after Attorney General Ken Cuccinelli II (R) wrote letters to state colleges and universities telling them that they did not have the legal authority to ban discrimination against gays and lesbians. Instead of an executive order, McDonnell ended up issuing an executive directive that state employees who discriminate based on sexual orientation would face punishment.

“Virginia has had an anti-gay reputation that’s intensified in recent years,” Ebbin added. “If we can pass these commonsense protections, we will move in the right direction.”

Earlier this month, the General Assembly voted to make Tracy Thorne-Begland the first openly gay person elected to a judgeship in the state, after his nomination was blocked amid controversy last year.