Just some background about my application. I am developing an ASP.Net MVC 3 Web App which uses Entity Framework 4.1 for data persistence. My application is tiered in that it has a UI layer, Service layer, Repository layer etc. I also use Unity for my Inversion of Control Container.
When a user registers on my application, I create two random codes (Email and Mobile verification codes) using the StringBuilder. I then assign these two random codes to their appropriate properties in a User Object, see below.
User validateUser = new User();
validateUser.firstName = model.firstName.Trim();
validateUser.lastName = model.lastName.Trim();
validateUser.email = model.Email.Trim();
//Create Email and Mobile Verification Codes
string randomEmailCode = "";
randomEmailCode = _notifyService.GenerateEmailCode();
validateUser.emailVerificationCode = randomEmailCode;
string randomMobileCode = "";
randomMobileCode = _notifyService.GenerateMobileCode();
validateUser.mobileVerificationCode = randomMobileCode;
NotifyService
public string GenerateEmailCode()
{
StringBuilder builder = new StringBuilder();
builder.Append(RandomString(4, true));
builder.Append(RandomNumber(1000, 9999));
builder.Append(RandomString(2, false));
return builder.ToString();
}
public string GenerateMobileCode()
{
StringBuilder builder = new StringBuilder();
builder.Append(RandomString(3, true));
builder.Append(RandomNumber(1000, 9999));
builder.Append(RandomString(2, false));
return builder.ToString();
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
private string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
I then add the user to my DBcontext and call the SaveChanges() method to save the new User into my database.
_accountService.AddUser(validateUser);
_accountService.Save();
Now, when I go into my database and find the new User which has been added, the emailVerificationCode and the mobileVerificationCode are both the same. However, if I put a break point on my code at the following line
randomEmailCode = _notifyService.GenerateEmailCode();
And trace through until the Save, and then go check the database for the new User, both emailVerificationCode and the mobileVerificationCode codes are different, as expected.
I cannot understand why when I run the application it inserts the same code for both properties.
Can anyone please help with this?
Thanks.
UPDATE
I did as Jane suggested, ie, put in hard coded values for each property like so
validateUser.emailVerificationCode = "emailCode";
validateUser.mobileVerificationCode = "mobileCode";
Ran the application again, and this time the two hardcoded values were inserted as expected. Does this mean that that my two methods GenerateEmailCode() and GenerateMobileCode() are not working correctly?