I'd like to create a Review
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 ProfileId
in the Profile
table. So I'm not sure if the syntax is correct here for creating the entities and their relationships?
Here is my code
public class Profile
{
public virtual ICollection<Review> 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; }
}
When I try to insert here is what I do.
var reviewer = dbContext.Profiles.Where(i => i.ApplicationUserId == userId).First();
Review reviewer = new Review
{
Created = DateTime.Now,
ReviewerProfile = reviewer
};
profile.Reviewers.Add(reviewer);
// then save...