title: Separating IdentityUser from UserProfile
body: <p>I split my user-details into it's own table <strong>Profile</strong> with a foreign key to the ApplicationUser, but now my inserts fail.</p>
  
  <p>So I got the following tables:</p>
  
  <pre><code>AspNetUsers 
  {
      Id
      UserName
      PasswordHash
      SecurityStamp
      Descriminator
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Profiles
  {
      Id
      FirstName
      LastName
      Email
      AspNetUser_Id
  }
  </code></pre>
  
  <p>So I changed the <strong>RegisterViewModel</strong> by adding the extra fields like this:</p>
  
  <pre><code>public class RegisterViewModel
  {
      [Required]
      [Display(Name = "User name")]
      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; }
  
      [Required]
      [Display(Name = "First name")]
      public string FirstName { get; set; }
  
      [Required]
      [Display(Name = "Last name")]
      public string LastName { get; set; }
  
      [Required]
      public string EMail { get; set; }
  }
  </code></pre>
  
  <p>... and incorporated the fields from the Profiles table into the <strong>Register.cshtml</strong> and I want to store the information from the model into the two tables. So my AccountController.Register looks like this:</p>
  
  <pre><code>    //
      // POST: /Account/Register
      [HttpPost]
      [AllowAnonymous]
      [ValidateAntiForgeryToken]
      public async Task&lt;ActionResult&gt; Register(RegisterViewModel model)
      {
          if (ModelState.IsValid)
          {
  
              var user = new ApplicationUser() { UserName = model.UserName };
              var result = await UserManager.CreateAsync(user, model.Password);
  
              if (result.Succeeded)
              {
                  var profile = new Profile
                  {
                      FirstName = model.FirstName,
                      LastName = model.LastName,
                      EMail = model.EMail,
                      User = user
                  };
  
                  var db = new DecoupleDb();
                  db.Profiles.Add(profile);
                  db.SaveChanges();
  
                  await SignInAsync(user, isPersistent: false);
                  return RedirectToAction("Index", "Home");
              }
              else
              {
                  AddErrors(result);
              }
          }
  </code></pre>
  
  <p>But now I keep getting an error on the </p>
  
  <pre><code>db.SaveChanges();
  </code></pre>
  
  <p>the error saying Im trying to violate the Primary key of the AspNetUsers table</p>
  
  <p>"Violation of PRIMARY KEY constraint 'PK_dbo.AspNetUsers'. Cannot insert duplicate key in object 'dbo.AspNetUsers'. The duplicate key value is (372775e0-bad5-452f-b65c-cd9bf7b80b34).
  The statement has been terminated."</p>
  
  <p>And I cant understand why, because it's only a foreign key so when I add the Profile the AspNetUser should not be created, or so I thought.</p>
  
  <p>Anyone see a fix?</p>