I would imagine you are having your problem because you can't perform the switch without causing the duplicate. The best solution I can think of off the top of my head is to set one to a temp value that would never be set in the key, perform the update of the other entity and then set the temp value back to what you originally wanted.
It's a little picky how it has to work though. The first entity to be saved in the collection needs to be saved as the 9999 temporary value. So A must be in the collection before B.
The field g is the unique key, or the portion that you are changing.
int originalValue = B.g;
B.g = A.g;
A.g = 99999;
collection.SaveMulti(true);
A.g = originalValue;
A.Save();
This seems like a bad hack, if someone has a better solution don't hesitate to chime in. But this is the first thing that came to mind.