In my case the S character inside the connection string value from appsettings.json was not escaped correctly.
I had "Data Source=MYACER\SQL2022MYDB...." and after correcting the \ character to be double \ it worked ok.
There was also an error indicated in Visual Studio for appsettings.json file about this which I did not notice initially. So this falls into incorrect syntax in appsettings.json for the connection string, make sure the syntax is ok in there.