title: EF Code First The INSERT statement conflicted with the FOREIGN KEY constraint
body: <p>When I try to insert a record in the database, I get this error message:</p>
  
  <blockquote>
    <p>The INSERT statement conflicted with the FOREIGN KEY constraint
    "FK_dbo.Order_dbo.AspNetUsers_UserId". The conflict occurred in
    database "Test", table "dbo.AspNetUsers", column 'Id'. The
    statement has been terminated.</p>
  </blockquote>
  
  <p><em>Disclaimer: I know that this question has been asked before (<a href="https://stackoverflow.com/questions/15185991/in-code-first-the-insert-statement-conflicted-with-the-foreign-key-constraint">here</a>, <a href="https://stackoverflow.com/questions/19828467/ef-code-first-the-insert-statement-conflicted-with-the-foreign-key-constraint">here</a> and <a href="https://stackoverflow.com/questions/15584278/what-causes-the-insert-statement-conflicted-with-the-foreign-key-constraint">here</a>), but none of answers helped me fix this error I get.</em></p>
  
  <p>Here are the models:</p>
  
  <pre><code>[Table("Order")]
  public class Order
  {
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity), Key]
      public int Id { get; set; }
  
      [Required]
      public string UserId { get; set; }      
  
      public virtual List&lt;OrderDetails&gt; OrderDetails { get; set; }
  
      [ForeignKey("UserId")]
      public virtual ApplicationUser User { get; set; }
  }    
  
  [Table("OrderDetails")]
  public class OrderDetails
  {
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity), Key]
      public int Id { get; set; }
  
      [Required]
      public int OrderId { get; set; }
  
      [ForeignKey("OrderId")]
      public virtual Order Order { get; set; }
  }
  
  public class ApplicationUser : IdentityUser
  {
      [MaxLength(15)]
      public string FirstName { get; set; }
  
      [MaxLength(15)]
      public string LastName { get; set; }
  
      [MaxLength(50)]
      public string EmailAddress { get; set; }
  
      public virtual List&lt;Order&gt; Orders { get; set; }
  }
  </code></pre>
  
  <p>Code that inserts the new <code>Order</code>:</p>
  
  <pre><code>public OrderDetails Add(dynamic addOrderDetails, string userId)
  {
      if (addOrderDetails.OrderId == null)
      {
          var order = new Order()
          {
              UserId = userId,
              CreateDate = DateTime.Now,
              Status = OStatus.InProgress,
              Confirmed = (DateTime?)null,
          };
  
          var newOrder = _dbContext.Orders.Add(order);
          _dbContext.Entry(order).State = EntityState.Added;
          _dbContext.SaveChanges(); // this is where the error msg is being thrown.
  
          var orderDetails = new OrderDetails()
          {
              OrderId = newOrder.Id,
              ServiceId = addOrderDetails.ServiceId,
              EventStart = start,
              EventEnd = end
          };
  
          var newOrderDetails = _dbContext.OrderDetails.Add(orderDetails);
          _dbContext.Entry(orderDetails).State = EntityState.Added;
          _dbContext.SaveChanges();
      }
  
      return null;
  }
  </code></pre>
  
  <p>The error is being thrown at this line: <code>_dbContext.SaveChanges(); // this is where the error msg is being thrown.</code></p>
  
  <p>When debugging I can see that <code>UserId = userId,</code> has a value.</p>
  
  <p>So why am I getting this error message?</p>