Error registering user

1

When I try to create a second user, with ASP.NET MVC , I get an error:

  

Violation of PRIMARY KEY constraint 'PK_dbo.AspNetUsers'. Can not insert duplicate key in object 'dbo.AspNetUsers'. The duplicate key value is (00000000-0000-0000-0000-000000000000).   The statement has been terminated.

My controller looks like this:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new Usuario { Id = new Guid(), UserName = model.UserName, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

and my ViewModel :

 public class RegisterViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [Display(Name = "Usuário")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

What am I doing wrong?

    
asked by anonymous 26.12.2016 / 20:16

1 answer

4

Try changing Id = new Guid() by Id = Guid.NewGuid() . The new Guid will create a new key with zeros, and as you already must have a registered user happens the duplicate primary key exception.

See working at dotnet fiddle .

    
26.12.2016 / 20:33