title: How do I create a review entity as a child of a parent entity in Entity Framework
body: <p>I'd like to create a <code>Review</code> entity so users can review and rate a user (profile) entity for my social site. But the problem I'm having is when trying to insert a review into the table I get an exception; something about a foreign key conflicting with <code>ProfileId</code> in the <code>Profile</code> table. So I'm not sure if the syntax is correct here for creating the entities and their relationships?</p>
  
  <p>Here is my code</p>
  
  <pre><code>public class Profile 
  {
      public virtual ICollection&lt;Review&gt; Reviews { get; set; }    // one to many
  }
  
  public class Review
  {
      [Key]
      public int ReviewId { get; set; }
  
      public int ProfileRefId { get; set; }
  
      // one-to-many here
      [ForeignKey("ProfileRefId")]
      public virtual Profile Profile { get; set; }
  
      public DateTime Created { get; set; }
      public string body {get; set; }
      public int Rating {get; set; }
  
      // here is where I want to set the reviewer to a user (profile entity), 
      //   but not sure about the syntax
      [ForeignKey("ReviewerProfile ")]
      public int ProfileId { get; set; }
  
      public virtual Profile ReviewerProfile { get; set; }
  }
  </code></pre>
  
  <p>When I try to insert here is what I do.</p>
  
  <pre><code>var reviewer = dbContext.Profiles.Where(i =&gt; i.ApplicationUserId == userId).First();
  Review reviewer = new Review
                        {
                            Created = DateTime.Now,
                            ReviewerProfile = reviewer
                        };
  
  profile.Reviewers.Add(reviewer);
  
  // then save...
  </code></pre>