I would like to do mapping using the Fluent API. I saw some other examples but these look different to what I want to do.
Here is my SQL:
ALTER TABLE [Question] ADD CONSTRAINT [FK_QuestionUserProfile]
FOREIGN KEY([AssignedTo]) REFERENCES [UserProfile] ([UserId])
Which gives me this error message:
There are no primary or candidate keys in the referenced table 'UserProfile'
that match the referencing column list in the foreign key 'FK_QuestionUserProfile'
Could not create constraint
I have two classes:
public class Question
{
public int QuestionId { get; set; }
public string Text { get; set; }
public int AssignedTo { get; set; }
public int ModifiedBy { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
public partial class UserProfile
{
public UserProfile()
{
this.webpages_Roles = new List<webpages_Roles>();
}
public int UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
I want to do the mapping between Question <> UserProfile so that:
- AssignedTo is mapped to UserId in UserProfile.
- Modified is mapped to UserId in UserProfile.
I have this so far in my QuestionMap
this.HasRequired(t => t.UserProfile)
.WithMany(t => t.Questions)
.HasForeignKey(d => d.AssignedTo);
this.HasRequired(t => t.UserProfile)
.WithMany(t => t.Questions)
.HasForeignKey(d => d.ModifiedBy);
But will this work as the ForeignKey has AssignedTo as the name in Question and UserId in UserProfile. Is there a way in the mappings that I can specify they should map to UserId ?