I have a registration table in my database with a field RegistrationNumber
which needs to be unique.
I am wondering about what is the best way to guarantee that I will provide unique number in every case.
What I do is in my Repository
I save the new Registration, something like:
void IMyRepository.Repository(Registration registration)
{
registration.RegistrationNumber = _getNewRegistrationNumber();
dbContext.SaveChanges();
}
private string _getNewRegistrationNumber()
{
// what to do? get last registration number and increment? it could be
// either integers or integers mixed with letters.
}
What I'm worried about is if two people complete the registration form at the same time, I'm afraid that before the first one arrives at dbContext.SaveChanges();
the second one will enter the _getNewRegistrationNumber()
function and might get the same RegistrationNumber
.
Any advise?
[EDIT] : GUID is too long