title: How to auto-generate objects in the Register Model C# ASP.NET MVC 4?
body: <p>I using the Internet Application template in C# MVC 4 which generates the <code>Account</code> model and controller for you in Visual Studio. This gives me basic form functionality for logging in. I modified the <code>Register</code> class in the <code>Account</code> model to also take a <code>FirstName</code>, <code>LastName</code>, and <code>Email</code> in addition to the username and password. My table with user information is <code>Users</code>. When a user submits their information <code>WebSecurity.CreateUserAndAccount(model.UserName, model.Password);</code>
  is ran and it adds the <code>Username</code> to my Users table. So at that point I have my PK <code>Id</code> and <code>Username</code> in the table. My <code>User</code> class definition has a custom object <code>Portfolio</code>. What I am trying to do is when the user registers they get added to the table and then I find that <code>User</code> and set the values for the rest of the data in the table and instantiate the <code>Portfolio</code> object. This would complete my register process.Currently, it adds the user to the table with just the <code>Username</code> adds them to the role and redirects back to index. My var user = <code>_db.Users.Find(userId)</code> is apparently not finding anything for some reason. I even tried to cheat and hardcode the <code>userId</code> because I knew what the next <code>Id</code> would be in the <code>Users</code> table and it still did not modify the table values. <code>FirstName</code>, <code>LastName</code>, <code>Email</code> all remain null in the table and no object is instantiated for <code>Portfolio</code>. I have my context so it should work. I did something similar in my <code>Seed</code> method and it worked fine. I am not sure why it is not working here. Does anyone have suggestions? Thank you.  </p>
  
  <pre><code>    [HttpPost]
      [AllowAnonymous]
      [ValidateAntiForgeryToken]
      public ActionResult Register(RegisterModel model, TeamProject.Models.ProjectDb2 context)
      {
          if (ModelState.IsValid)
          {
              // Attempt to register the user
              try
              {
                  WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
  
                  var userId = _db.Users
                               .Where(u =&gt; u.UserName == model.UserName)
                               .Select(u =&gt; u.Id).First();
                  var user = _db.Users.Find(userId);
                  user.FirstName = model.FirstName;
                  user.LastName = model.LastName;
                  user.Email = model.Email;
                  user.Portfolio = new Portfolio
                  {
                      Stocks = new List&lt;Stock&gt;
                      {
                      }
                  };
                  context.Users.AddOrUpdate(user);
  
                  System.Web.Security.Roles.AddUserToRole(model.UserName, "Users");
                  WebSecurity.Login(model.UserName, model.Password);
                  return RedirectToAction("Index", "Home");
              }
  </code></pre>