For a school assignment I want to make a small questionaire-type of website where you can input several questions and add them to an exam.
The technologies I use are MVC and Entity Framework (code first). For both it is the first time I am riding on my own, without tutorials and such and I seem to have gotten stuck in my model-design (those that will be used for the database).
I want an Exam
table which holds several Questions
. This, to my understanding, should be a collection (research said to use this, but I am unsure this is correct).
How would I set the Exam
model in this case, so that it holds multiple Question objects
and can also be used by Entity Frameworks' Code First?
Here is what I have now:
public class Exam
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreationDate { get; set; }
public ICollection<Question> Questions { get; set; }
}
public class Question
{
public enum Answers
{
A,
B,
C,
D
}
public int Id { get; set; }
public string Name { get; set; }
public string AnswerA { get; set; }
public string AnswerB { get; set; }
public string AnswerC { get; set; }
public string AnswerD { get; set; }
public Answers Correct { get; set; }
}
You may want add to your Question properties like this:
[ForeignKey("Exam")]
public int ExamId { get; set; }
public virtual Exam Exam { get; set; }
if you want to have control over foregin keys.
And you can add virtual keyword to enable lazy loading in your Exam class:
public virtual ICollection<Question> Questions { get; set; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.