title: ASP.NET MVC 5 ApplicationUser as foreign key
body: <p>What I would like to be able to do is update the UserContent table any time a user visits a content page. I created a controller to try and manually input data before implementing this but I am getting a foreign key error:    </p>
  
  <pre><code>The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.UserContent_dbo.IdentityUser_ApplicationUserId". The conflict occurred in database "COMPILING_a6a38dac907c4816af896bbeb6cdaa55", table "dbo.IdentityUser", column 'Id'.
  The statement has been terminated.
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>public class UserContent
  {
      public int ID { get; set; }
  
      [Display(Name = "Times Visited")]
      public int Visited { get; set; }
  
      [Display(Name = "Rating")]
      public byte Rating { get; set; }
  
      [Display(Name = "Content Page")]
      public int ContentID { get; set; }
  
      [Display(Name = "User")]
      public string ApplicationUserId { get; set; }
  
      public virtual Content Content { get; set; }
      public virtual ApplicationUser ApplicationUser { get; set; }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>private CompilingContext db = new CompilingContext();
  private ApplicationDbContext db2 = new ApplicationDbContext();  
  
  public ActionResult Create([Bind(Include="ID,Visited,Rating,ContentID,ApplicationUserId")] UserContent usercontent)
      {
          if (ModelState.IsValid)
          {
              db.UserContent.Add(usercontent);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          ViewBag.ApplicationUserId = new SelectList(db2.Users, "Id", "UserName", usercontent.ApplicationUserId);
          ViewBag.ContentID = new SelectList(db.Contents, "ID", "Title", usercontent.ContentID);
          return View(usercontent);
      }
  </code></pre>