0

I have a Answer table which has pk_answerid, answertext , fk_questionid, chosenoptions stored as 1,3,2 (comma separated)

answertext if populated, then chosenoptions null and if chosenoptions if populated then answertext is null.

ChosenOption table has structure pk_chosenoptionid, fk_questionid In EntityFramework 4, I have something along these lines

void SaveAnswers(ICollection<Answer> answers)
{
        context.Answers.Add(answers);
        context.SaveChanges();
}

This works fine....but when in the UI I go back to page which is wizard with Q&A's on different pages and I deselect all answers previously selected, and click save, no answers are deleted. I also tried DeleteObject which does not delete the Answers and chosenoptions even though I have cascade delete on the Answer table to Chosen option table. Also in edit scenarios if for e.g. if the User selected option 1,2 and then saves it and then goes back and selects 3,2 how do you write code in EF to do such complex stuff. I haven't come across any tutorials which explain such scenarios. Most of what I have seen is simple add, delete and applypropertychanges. I have an MVC app, which has lazy loading enabled. Pls help and suggest some sort of code using an example or any pointers to existing blogs where this is explained.

chugh97
  • 9,602
  • 25
  • 89
  • 136
  • 1
    Add definition (code) of your involved entities - Question, Answer, Options where navigation properties, PKs and FKs will be visible. Also add code used to update answers. – Ladislav Mrnka Jun 04 '11 at 13:47
  • What do you mean by definition code? – chugh97 Jun 04 '11 at 16:17
  • 1
    I mean code of your entities which will show how they look like because your description is totally unclear. Also just in case: how do you handle steps in your wizard? You don't save changes after each step, do you? – Ladislav Mrnka Jun 04 '11 at 16:18
  • I do save changes aftereach step. Also use can go back change answers in which case at the moment I delete all answers to a question and re-add the answers. This does not seem right as the Ef should take care??? – chugh97 Jun 04 '11 at 21:45

1 Answers1

2

EF doesn't take care - I shame to posting this again and again but I really don't like to explain it every time: Update relationships when saving changes of EF4 POCO objects

Check that explanation. Your problem is similar. You persisted entity graph (set of related entities) and now you want to change the graph. Your new graph is extracted from web request which mean it is detached and EF doesn't know what has changed. You must do it manually - I just discussed what does it mean to do it manually in another question = it is usually too complex.

The easiest way is your current approach - delete everything and add it again but it is really ugly. Another approach is loading the graph from database and manually merge all changes you get from the request = compare these two graph and update the loaded (attached). Be aware that you must manually call DeleteObject for anything you want to delete from the database. Then just call SaveChanges.

Btw. if you are doing wizard why don't you just store current state in session and save everything once user completes the wizard? You will avoid all these complications.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670