I'm creating an online quiz application where many users will take the same exam with multiple choice questions. Users will answer the same questions of the same exam.
I have the following models:
Exam
public class Exam
{
public Exam()
{
Questions = new HashSet<Question>();
}
public int Id { get; set; }
public string Name { get; set; }
public DateTime ExamDate { get; set; }
public ICollection<Question> Questions { get; set; }
}
Question
public class Question
{
public Question()
{
Answers = new HashSet<Answer>();
}
public int Id { get; set; }
public string Description { get; set; }
public int ExamId { get; set; }
public TimeSpan? Remainedtime { get; set; }
public int Score { get; set; }
public ICollection<Answer> Answers { get; set; }
public ICollection<UserQuestion> UserQuestions { get; set; }
}
public class ExamUser: IdentityUser
{
public int Result { get; set; }
public ICollection<UserQuestion> UserQuestions { get; set; }
}
UserQuestion
public class UserQuestion
{
public int ExamUserId { get; set; }
public ExamUser ExamUser { get; set; }
public int QuestionId { get; set; }
public Question Question { get; set; }
}
As you can see, there is many-to-many relation between Examusers and Questions as each user will have say 20 exam questions where each question will get different result for each examuser.
For this I created onmodelcreating which is also must be ok.
public class IntellectDbContext:IdentityDbContext<ExamUser>
{
public IntellectDbContext(DbContextOptions<IntellectDbContext> dbContextOptions) : base(dbContextOptions)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<UserQuestion>()
.HasKey(a => new { a.ExamUserId, a.QuestionId });
modelBuilder.Entity<UserQuestion>()
.HasOne(a => a.ExamUser)
.WithMany(b => b.UserQuestions)
.HasForeignKey(a => a.ExamUserId);
modelBuilder.Entity<UserQuestion>()
.HasOne(a => a.Question)
.WithMany(c => c.UserQuestions)
.HasForeignKey(a => a.QuestionId);
}
public DbSet<Answer> Answers { get; set; }
public DbSet<Question> Questions { get; set; }
public DbSet<Exam> Exams { get; set; }
public DbSet<ExamUser> ExamUsers { get; set; }
public DbSet<UserQuestion> UserQuestions { get; set; }
}
The problem is that when calculating result for each user questions, I don't know how to address them in relevant action. I need a correct way of something like this:
public IActionResult QuestionScore()
{
if(_SignInManager.IsSignedIn(User){
foreach(Answer item in Questions){
if(item.Correct) {
//main needed part to fix => UserQuestion.ExamUser.Question.Score = 1; (for ex.)
The relationship between your tables seems unclear to me, you want to calculate the the final result of your exam users?
FaridGN
UserQuestion.ExamUser.Question.Score = 1
In my understanding, the score of a question should be a static value, then why would it be reset to 1. I think the value which you want to change should be the result in your examuser, right?
A detailed answer may not be provided since the relation is confusing, but you can refer to assign value using linq for more information about how to set value with Linq.
Member
19 Points
127 Posts
Addressing User's Question using LINQ when there is many-to-many relationship between User and Qu...
Jan 11, 2020 06:26 PM|FaridGN|LINK
Hi guys,
I'm creating an online quiz application where many users will take the same exam with multiple choice questions. Users will answer the same questions of the same exam.
I have the following models:
Exam
Question
Answer
User
UserQuestion
As you can see, there is many-to-many relation between Examusers and Questions as each user will have say 20 exam questions where each question will get different result for each examuser.
For this I created onmodelcreating which is also must be ok.
The problem is that when calculating result for each user questions, I don't know how to address them in relevant action. I need a correct way of something like this:
public IActionResult QuestionScore()
{
if(_SignInManager.IsSignedIn(User){
foreach(Answer item in Questions){
if(item.Correct) {
//main needed part to fix => UserQuestion.ExamUser.Question.Score = 1; (for ex.)
P.S. Please, ignore minor syntax mistakes.
Contributor
3140 Points
983 Posts
Re: Addressing User's Question using LINQ when there is many-to-many relationship between User an...
Jan 14, 2020 05:47 AM|Yang Shen|LINK
Hi FaridGN,
The relationship between your tables seems unclear to me, you want to calculate the the final result of your exam users?
In my understanding, the score of a question should be a static value, then why would it be reset to 1. I think the value which you want to change should be the result in your examuser, right?
A detailed answer may not be provided since the relation is confusing, but you can refer to assign value using linq for more information about how to set value with Linq.
Best Regard,
Yang Shen
None
0 Points
4 Posts
Re: Addressing User's Question using LINQ when there is many-to-many relationship between User an...
Feb 21, 2020 05:37 PM|neeapps|LINK
Thank you
https://www.smular.com/