The user(AskedUser) can have many questions asked by other users(Asker). Users(Asker) can ask questions to other users(AskedUser). So the QuestionModel should have foreign key to asked user id and foreign key to user who asked the question.
Do I constructed my models to what I want to achieve? How to configure this using fluent api cause this is not achievable using data annotations only.
public class ApplicationUser : IdentityUser<long>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string Country { get; set; }
public DateTime BirthDate { get; set; }
public ICollection<QuestionModel> AskedQuestions { get; set; }
}
public class QuestionModel
{
public int Id { get; set; }
public string Content { get; set; }
public bool IsAnswered { get; set; }
public long AskerId { get; set; }
public ApplicationUser Asker { get; set; }
public long AskedUserId { get; set; }
public ApplicationUser AskedUser { get; set; }
}
This is I've tried so far
builder.Entity<ApplicationUser>()
.HasMany(user => user.AskedQuestions)
.WithOne(q => q.AskedUser)
.HasForeignKey(user => user.AskedUserId)
.HasConstraintName("ForeignKey_User_AskedQuestion")
.HasForeignKey(user => user.AskerId)
.HasConstraintName("ForeignKey_Asker_QuestionAsked")
.IsRequired(true);