Add/Update list in database using Entity framework 6

asp.net-mvc-5 asp.net-web-api2 entity-framework entity-framework-6

Question

QuestionBank, Question, and Answer are the three tables I have. Both "QuestionBank" and "Question" will include lists of questions and answers.

QUESTIONBANK :-

public class QuestionBank
{
public int id { get; set; }
public string Text { get; set; }
public string Chapter { get; set; }
public string Standard { get; set; }
public List<Question> Question { get; set; }
public QuestionBank()
{
this.Question = new  List<Question>();
}
}

QUESTION:-

public class Question
{
public int id { get; set; }
public string Title { get; set; }
public string QuestionText { get; set; }
public List<Answer> Answer { get; set; }
public string CorrectAnswer { get; set; }
public Question()
{
    this.Answer = new List<Answer>();
}
}

ANSWER:-

public class Answer
{
public int id { get; set; }
public string AnswerText { get; set; }
}

/Edited WEB API

  private IRepository<QuestionBank> _QuestionBankRepository;  
  public QuestionController(IRepository<QuestionBank> QuestionBankRepository)
    {
        _QuestionBankRepository = QuestionBankRepository;  
    }
[HttpPost]
[Route("Ques/Add")]
    public Boolean Add(QuestionBank AddQuetionBankData)
    {
        var isQuetionBankPresent = _QuestionBankRepository.GetAll(p => p.Text == AddQuetionBankData.Text && p.Standard == AddQuetionBankData.Standard && p.Chapter == AddQuetionBankData.Chapter).FirstOrDefault<QuestionBank>();
        if (isQuetionBankPresent != null)
        {
            /* Add the data in Question and Answer tables  */
            return false;
        }
        else
        {
            /* Add the data in all three tables  */
            return true;
        }
    }

This database is what I have for the web api. Now I want to add the data to the database using JSON. If a row already exists in QuestionBank, I just want to add the data to the Question and Answer tables using the appropriate foreign keys. The entity framework and mvc 5 web api are what I'm utilizing. I'm now in a rut. Please let me know if anything else is required. I appreciate it.

1
0
7/14/2014 2:41:30 PM

Popular Answer

ZZZ_tmp
0
7/14/2014 6:33:48 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow