Before a crowd of hundreds at City Hall, the San Antonio City Council adopted a controversial ordinance Thursday aimed at preventing discrimination on the basis of sexual orientation and gender identity.

After hours of testimony, the council passed the ordinance on an 8-3 vote. Councilmembers Elisa Chan, Carlton Soules and Ivy Taylor were the three no votes.

San Antonio had been one of the only major Texas cities that did not offer protections for lesbian, gay, bisexual and transgender individuals. Austin, Fort Worth, Dallas and Houston all have similar policies in place.

The ordinance, which goes into effect immediately, prevents local businesses from discriminating against LGBT people. Before the ordinance, proponents claimed, they could be kicked out at a business owner’s discretion.

The ordinance also prevents public officials from discriminating against LGBT people while acting in their official duties and prohibits people who are awarded city contracts from discriminating against them.

“This city has a lot going for it, and I believe that this ordinance will help ensure that everybody in our city is treated equally,” said Mayor Julián Castro, who supported the ordinance. “You’re going to be treated the same way whether you’re Christian, white, Hispanic or ... part of the LGBT community.”

A court challenge is expected from conservative groups. In a letter to Castro on Wednesday, Attorney General Greg Abbott, a leading candidate for Texas governor, said "legal action will surely follow" the passage of the ordinance. Among other things, Abbott said it violated the First Amendment of the U.S. Constitution as well as the Texas Constitution by threatening to remove any appointed city official who discriminated against a person on the basis of sexual orientation or gender identity.

“The obvious problem with this provision is that it allows government to impose thought and speech control over any city official or board or commission member who may hold deep religious beliefs that are counter to the ordinance,” Abbott said.

The ordinance has been the talk of Texas politics over the past few weeks, with Abbott, U.S. Sen. Ted Cruz and state Sen. Donna Campbell, R-New Braunfels, among those speaking out against it. All three Republican candidates for state attorney general — state Rep. Dan Branch, R-Dallas; state Sen. Ken Paxton, R-McKinney; and Railroad Commission Chairman Barry Smitherman — also weighed in against it.

Jonathan Saenz, the president of the conservative group Texas Values, echoed Abbott's concerns about the impact of the ordinance on people of faith.

“We’ve seen how that has played out in other parts of the country where people have been persecuted for disagreeing with a homosexual lifestyle,” Saenz said. “People have been forced to photograph gay wedding ceremonies in states where they do not even recognize homosexual marriage.”

But Chuck Smith, the executive director of Equality Texas, which advocates for same-sex marriage, said the ordinance is about fairness and equality for all people. He said the threat of legal action is overblown.

“The ordinance is no different than ordinances that exist in other Texas cities and in almost 180 cities across the country,” Smith said. “Preventing discrimination is not unconstitutional. It is the right thing to do.”