Search code examples
c#asp.net-mvcrazorasp.net-identity

Identity is trying to duplicate the email property when I insert a user


So I have an asynchronous Task method where type T = ActionResult to create a new user to be stored in the database (DBMS = SQL Server). This used to work perfectly before without any errors but all of a sudden it starts breaking when it gets to this point in execution:

 var result = await UserManager.CreateAsync(user, model.Password);

I have reverted everything to the working version on Git but still no luck.

What could be the problem and how can I get around this?

Controller:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser()
        {
            UserName = model.UserName,
            firstName = model.firstName,
            lastName = model.lastName,
            Email = model.email,
            cellNo = model.cellNo,
            DateCreated = DateTime.Now
        };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            //await SignInAsync(user, isPersistent: false);
            //  util.Log(User.Identity.GetUserId(), 5, "Account - Added a user ( " + model.UserName + " ) to the system");
            return RedirectToAction("SuccessfullyAddedNewUser", "Users", model);
        }
        else
        {
            AddErrors(result);
        }
    }
    // If we got this far, something failed, redisplay form
    return View(model);
}

//Users controller action method to get redirected to once new user has been created successfully
// GET: /Users/SuccessfullyAddedNewUser
public ActionResult SuccessfullyAddedNewUser(RegisterViewModel model)
{
    return View(model);
}

Model: ApplicationUser

public class ApplicationUser : IdentityUser
{

    [Required]
    [StringLength(11, MinimumLength = 11)]
    [Display(Name = "Cell Number")]
    public string cellNo { get; set; }

    [Required]
    [MaxLength(50), MinLengthAttribute(3)]
    [Display(Name = "First Name")]
    public string firstName { get; set; }


    [Display(Name = "Email Address")]
    public string email { get; set; }

    [Required]
    [MaxLength(50), MinLengthAttribute(3)]
    [Display(Name = "Last Name")]
    public string lastName { get; set; }

    [Display(Name = "Date Created")]
    public DateTime DateCreated { get; set; }

    [Required]
    [DefaultValue(false)]
    public Boolean MustResetPassword { get; set; }

    //Concatenates the first and last name
    public String getFullName()
    {
        return firstName + " " + lastName;
    }
}

Exception:

Exception: Message = "An error occurred while updating the entries. See the inner exception for details."

Inner Exception: The column name 'email' is specified more than once in the SET clause or column list of an INSERT. A column cannot be assigned more than one value in the same clause. Modify the clause to make sure that a column is updated only once. If this statement updates or inserts columns into a view, column aliasing can conceal the duplication in your code.

But when I check my code, I only specify email once only.


Solution

  • IdentityUser already has an Email property. Your ApplicationUser is duplicating that property with email regardless of case.

    Remove the duplicated property

    [Display(Name = "Email Address")]
    public string email { get; set; }
    

    from ApplicationUser or override it with

    [Display(Name = "Email Address")]
    public new string Email { get; set; }
    

    if the intention was just to add the display attribute.