Ho tre tavoli QuestionBank, domande e risposte. "QuestionBank" avrà l'elenco delle domande e "Domanda" avrà l'elenco di "Risposta".
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>(); } }
DOMANDA: -
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>(); } }
RISPOSTA :-
public class Answer { public int id { get; set; } public string AnswerText { get; set; } }
API WEB: - // Modificato
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; } }
Ho questo database per il web API. Ora voglio aggiungere i dati nel database tramite json {"QuestionBank": QuestionBank, "Domanda": Domanda, "Risposta": Risposta} se la riga è presente in QuestionBank non voglio aggiungere quei dati nella tabella di QuestionBank e aggiungere solo i dati nella tabella delle domande e delle risposte con le rispettive chiavi esterne. Sto usando l'entità frame work e mvc 5 web api. Sono bloccato a questo punto. Per favore, se qualcosa è necessario fammi sapere. Grazie in anticipo.
Il modo di aggiornare Entity Framework è quello di Context.Entry([your object]).State = System.Data.Entity.EntityState.Modified;
a condizione che l'oggetto sia del tipo giusto.
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;
}
L'EF aggiorna l'entità (la classe che si passa al metodo Entry()
) in base al suo tipo.
Si noti che la posizione di SaveChanges()
: funziona come una stored procedure, si fanno tutti gli aggiornamenti e SaveChanges()
è come il comando SQL COMMIT
.
Dovresti anche racchiudere SaveChanges in un try / catch per gestire gli errori e disporre il _context.
MODIFICARE
Questa classe ha come dipendenza IRepository<Question>
, IRepository<QuestionBank>
, e IRepository<Answer>
.
Dovresti creare un UpdateController (o PublishController o qualsiasi altra cosa) che ottenga le tre dipendenze nel costruttore (meglio un servizio di facciata) e chiama il metodo Add()
per ognuna di esse.
Se si accede direttamente all'oggetto Database raw si potrebbe fare come ho fatto io e usare il metodo Entry()
per ogni tabella.