Riverside Church has signed a contract to buy a historical dormitory from Morningside Heights neighbor Union Theological Seminary for about $45 million.

Back in May, we told you that Craig Nassi of BCN Development was in contract for McGiffert Hall, the elaborate Gothic stone building at 97 Claremont Ave. on the southwest corner of West 122nd Street.

But there was a spoiler. When John D. Rockefeller donated the property to UTS, he added restrictions that detailed elaborate appraisals along with a right of first offer to the adjacent Riverside Church, which fronts on Riverside Drive.

That offer was also extended to any Rockefeller descendant still owning property on the block, but not one of them does anymore.

Nassi, who had been working on this deal with UTS for three years, even lined up financing and worked on redevelopment plans.

Meanwhile, Riverside Church brought in Cushman & Wakefield to provide the appraisals. In mid-summer, sources said, Riverside signed the contract with Union Theological, pushing Nassi aside.

As part of the transaction, Union Theological will lease back McGiffert Hall for its faculty and student housing for up to five years until a replacement is constructed.

UTS is also facing a $150 million tab for its own campus upgrades.

“This sale will help us make crucial upgrades to our campus and provide more accessible access to our campus for our students and the community who visit Union,” Rev. Dr. Serene Jones, president of UTS, said in a statement.

In the interim, Riverside will figure out what to do with McGiffert Hall. It may include an expansion of social services, which include housing and a large food pantry, sources said.

The sale should close in the next few weeks.