This is really starting to get to me. Basic enough , I want to prevent the user from registering with an email which already exist's in the UserProfile table.
I tried the following in my Register action in Account Controller- using
private UsersContext db = new UsersContext();
UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserEmail.Equals(useremail));
if(user != null)
{
ModelState.AddModelError("UserEmail", "User email already exists. Please enter a different email.");
}
from which I get this error- The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe
I also tried the Remote attribute (which worked for the UserName property)
[AllowAnonymous]
[HttpPost]
public JsonResult doesEmailExist(string UserEmail)
{
return Json(!db.UserProfiles.Any(user => user.UserEmail == UserEmail), JsonRequestBehavior.AllowGet);
}
And decorated the property with it-
[Remote("doesEmailExist", "Account", HttpMethod = "POST", ErrorMessage = "Email address already exists. Please enter a different Email address.")]
error- (inner exception) {"The column name is not valid. [ Node name (if any) = Extent1,Column name = UserEmail ]"}
Where am I going wrong here , I'm just looking for a simple check, even if it takes place on the form POST. Please note - this is an mvc 4 application using razor views and simpleMembership Thanks for looking.
UserProfile in AccountModel
public DbSet<UserProfile> UserProfiles { get; set; }
}
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string UserEmail { get; set; }
}
Add a unique key on the field UserEmail
.
alter table UserProfile add constraint UK_UserEmail Unique(UserEmail);
In your Register
action :
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { UserEmail = model.UserEmail});
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
catch (SqlException e)
{
if (e.Message.Contains("UK_UserEmail"))
{
ModelState.AddModelError("Error","Email cannot be duplicate");
}
}
I don't know if this could be considered as best practice, but hey, it works!