AlwaysLearningNewStuff AlwaysLearningNewStuff - 2 months ago 20
C# Question

Implementation of navigational properties for described data model

INTRODUCTION



I am trying to learn how to use Entity framework on my own ( Code First approach ) by solving a small task that I have designed myself.

In order to understand my problem, you must be familiar with the content of the task I mentioned, so i will provide relevant information in the below section.

RELEVANT INFORMATION:



I have invented the following data model for a small quiz:


  • Each player answers 10 questions.

  • Each question has 3 possible answers, user chooses one (by clicking on the radio button, for example)

  • Only one answer is correct, other 2 are wrong.



PROBLEM:



I got stuck at implementing POCOs, so I need your advice on how to implement them properly.

I believe I did the basic stuff properly and that my main problem is in implementing navigational properties.

MY EFFORTS TO SOLVE THIS:



I do not have much to show. Still, my habit is to always show everything I have, in order to ease the task of the community.

Therefore, these are my unfinished POCOs:

public class Answer
{
public int AnswerId { get; set; }
public string TextOfTheAnswer { get; set; }
}

public class Question
{
public int QuestionId { get; set; }

public string TextOfTheQuestion { get; set; }
}

public class Player
{
public int PlayerId { get; set; }

public string Name { get; set; }
}


During writing of this post, I am using Google to learn as much as possible to solve my problem. If I make any headway I will update this post accordingly.

QUESTIONS:




  • How should I implement navigational properties to mirror the relationships from my data model?

  • Additionally, is there a way for me to enforce some of the imposed restrictions ( each question has 3 options; player answers on 10 different questions; only one answer is correct answer to the question; and so on...)?


  • I apologize if these questions may sound trivial to someone experienced. I am just beginning with C# and Entity framework, and can not wait to write anything that works. I hope you can all relate. Thank you for your understanding.

    Answer

    As for the navigational properties, here's something to get you started (let me know if there's something I have missed):

    public class Answer
    {
      [Key]
      public int AnswerId { get; set; }
    
      public string TextOfTheAnswer { get; set; }
    
      public int QuestionId{get;set;}
    
      ForeignKey(nameof(QuestionId))]
      public virtual Question Question{get;set;}
    }
    
    public class Question
    {
      [Key]
      public int QuestionId { get; set; }
    
      public string TextOfTheQuestion { get; set; }
    
      public virtual ICollection<Answer> Answers{get;set;}
    
      public int CorrectAnswerId{get;set;}
    
      ForeignKey(nameof(CorrectAnswerId))]
      public virtual Answer CorrectAnswer{get;set;}
    }
    
    
    public class SessionQuestion
    {
      [Key]
      public int SessionQuestionId { get; set; }
    
      public int QuestionId{get;set;}
    
      ForeignKey(nameof(QuestionId))]
      public virtual Question Question{get;set;}
    
      public int PlayerAnswerId{get;set;}
    
      ForeignKey(nameof(PlayerAnswerId))]
      public virtual Answer PlayerAnswer{get;set;}
    
      public int TriviaSessionId { get; set; }
    
      ForeignKey(nameof(TriviaSessionId))]
      public virtual TriviaSession TriviaSession{ get; set; }
    }
    
    public class TriviaSession
    {
      [Key]
      public int SessionId { get; set; }
    
      public int PlayerId { get; set; }
    
      ForeignKey(nameof(PlayerId))]
      public virtual Player Player{ get; set; }
    
      public virtual ICollection<SessionQuestion> SessionQuestions{get;set;}
    }
    
    public class Player
    {
        [Key]
        public int PlayerId { get; set; }
    
        public string Name { get; set; }
    
        public virtual ICollection<TriviaSession> TriviaSessions{get;set;}
    }
    

    Basically, EF creates subclasses of your classes at runtime, so leaving the navigation properties virtual lets the EF classes override them and obtain the reference according to the key which resides in the property whose name is the string passed to the ForeignKey attribute's constructor (quite a mouthful, huh?).

    One to many navigation is easily created via declaring a virtual generic ICollection property.

    Note that this model enforces the fact that only one question is correct- by design. As for the other restrictions, it sounds like business logic rules, not something you should have your data layer enforce.