Add/Update list in database using Entity framework 6

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

Question

I have three tables QuestionBank,Question and Answer. " QuestionBank " will have list of Question and " Question " will have list of " Answer ".

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; }
}

WEB API :- //Edited

  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;
        }
    }

I have this database for the web api. Now I want to add the data in database through json { "QuestionBank": QuestionBank, "Question": Question, "Answer": Answer } if the row is present in QuestionBank i dont want to add that data in QuestionBank table and only add the data in Question and Answer table with respective foreign keys. I am using the entity frame work and mvc 5 web api. I am stuck at this point. Please if any thing is needed let me know. Thanks in advance.

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

Popular Answer

The Entity Framework way to update is to to Context.Entry([your object]).State = System.Data.Entity.EntityState.Modified; providing that the object is of the right type.

public Boolean Add(QuestionBank AddQuetionBankData)
{

    bool flag = false;

    var question = this.MapToQuestion(AddQuetionBankData); //map the input to the EF Type Question
    var anwer = this.MapToAnswer(AddQuetionBankData); //map the input to the EF Type Answer

    var isQuetionBankPresent = _QuestionBankRepository.GetAll(p => p.Text == AddQuetionBankData.Text && p.Standard == AddQuetionBankData.Standard && p.Chapter == AddQuetionBankData.Chapter).FirstOrDefault<QuestionBank>();
    if (isQuetionBankPresent != null)
    {
        _context.Entry(question).State = EntityState.Modified;
        _context.Entry(answer).State = EntityState.Modified;
        /* Add the data in Question and Answer tables  */
        flag = false;
    }
    else
    {
        _context.Entry(question).State = EntityState.Modified;
        _context.Entry(answer).State = EntityState.Modified;
        _context.Entry(AddQuetionBankData).State = EntityState.Modified;
        /* Add the data in all three tables  */
        flag = true;
    }

    _context.SaveChanges();

    return flag;
}

private Question MapTo Question(QuestionBank q) //do this for Answers too
{
    var _q = _context.Question.Where(a=>a.Id == q.Id).SingleOrDefault();

    if(_q!=null)
    {
          _q.id = q.id; //this is already true
          _q.Title = q.Title;
          _q.QuestionText = q.Standard; //I guess
    }

    return _q;
 }

The EF updates the Entity (the class you pass to the method Entry()) accordingly to its Type.

Notice that the position of the SaveChanges(): it works like a stored procedure, you do all the updates and the SaveChanges() is like the SQL COMMIT command.

You should also wrap the SaveChanges in a try/catch to handle errors, and dispose the _context.

EDIT

This class has as dependency IRepository<Question>, IRepository<QuestionBank>, and IRepository<Answer>.

You should create an UpdateController(or PublishController or whatever) that gets the three dependencies in the constructor (better a Facade Service), and call the Add() method for each one of them.

If you access directly the raw Database object you could do like I did and use the Entry() method for each table.

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