0

Currently we're running a MySQL Database on a WampServer on localhost. We have a few classes and have mapped them to the database tables using POCO classes.

We have a repository for dealing with all creates, edits and deletes as is standard. We pass this repository a Question object, this question object has basic properties such as an id, text and also it contains a navigation property to link to all the answers to that question.

In the poco class of question all of the answers are stored as an ICollection. This has been working fine for weeks able to update and delete and create with no problems.

Now all of a sudden any time we try to edit a question it is leading to an exponential duplication of its answers, 2 became 4 which became 8 and before we knew it we had nearly 60k answers as we were trying to solve this problem.

The code in the repository for editing the question:

 public void EditQuestion(Question question)
    {
        var tempquestion = Context.Questions.Single(q => q.Question_Id == question.Question_Id);

        tempquestion.Question_Help = question.Question_Help;
        tempquestion.Question_Text = question.Question_Text;
        tempquestion.Question_Type = question.Question_Type;

        context.SaveChanges();}

We've stepped through this so many times with breakpoints on every line, checking the answer property, it stays as the correct number throughout the entire process, then it hits context.saveChanges() and if we catch the result of that suddenly its duplicated.

Further Notes and requested code:

Yes i am using entity framework for this, i should have specified.

My poco class for the question is this:

public int Question_Id { get; set; }

    public string Question_Text { get; set; }

    public string Question_Help { get; set; }

    public string Question_Type { get; set; }

    [Display(Name = "Set ID")]
    public int Set_Id { get; set; }

    public Set Set { get; set; }

    private ICollection<Answer> _answers;
    public ICollection<Answer> Answers
    {
        get
        {
            if (_answers == null)
            {
                _answers = Arep.GetAnswers(this.Question_Id);
            }
            return _answers;
        }
        set
        {
            _answers = value;
        }
    } //a collection of answers belonging to this question

EDIT 2: I think i may now be a large step closer to the real issue. Thanks to the helpful link from CodeCaster i checked through my last queries, when it has 2 answers it only executes 2 insert statements, which is expected, but im ending up with 4 answers total, apparently the real issue here is that its adding new ones when answers are passed along with an edited question, which never used to happen.

Hoping the fix will be to send the edited question along with no answers tied to it, that way no new ones are inserted and no duplicates are made.

RhysW
  • 455
  • 1
  • 4
  • 16
  • What do the POCOs look like? What are you using for DAL? Entity Framework? – Steen Tøttrup Jul 24 '12 at 11:20
  • [Log your queries](http://stackoverflow.com/questions/650238/how-to-show-the-last-queries-executed-on-mysql), since there's nothing wrong with the code you posted. Are you sure the duplication originates there? – CodeCaster Jul 24 '12 at 11:21
  • @SteenT Yes entity framework, see edits for poco class – RhysW Jul 24 '12 at 11:49
  • @CodeCaster Yes im certain thats where the duplication is happening, stepped through debugging the entire program from start to finish a good 10 times atleast, many breakpoints and many vars specific to catching the property im watching. Everything works fine, it goes into the savechanges fine, and must get duplicated in there because the very next line i catch the answers and they are duplicated – RhysW Jul 24 '12 at 11:51
  • @RhysW but a lot of stuff can happen before that. Only at `Context.SaveChanges()` your entities are written to the database, so when you're moving stuff around somewhere else you won't see it going wrong until you call `SaveChanges()`. So my advice: take a look at the queries that are being executed, so you see which entries are the culprit. – CodeCaster Jul 24 '12 at 12:15
  • 1
    @CodeCaster Thankyou! you solved my problem, logging the executed queries allowed me to see the error of what i was doing, the context was assuming the entities in the answers icollection were different to the ones in the database so adding them, thus leading to duplication. simply emptied that property before adding the edited question and my problem goes away! much thanks – RhysW Jul 24 '12 at 12:20

2 Answers2

0

No idea what the issue is, can't read that out of your question, so no idea if this fixes the problem, but I would make the POCO like so:

public class Question {
    public int Question_Id { get; set; }
    public string Question_Text { get; set; }
    public string Question_Help { get; set; }
    public string Question_Type { get; set; }
    [Display(Name = "Set ID")]
    public int Set_Id { get; set; }
    public Set Set { get; set; }
    public virtual ICollection<Answer> Answers { get; set; }
}

Does the answers have an ID field? I guess they do, when they're used with EF.

Steen Tøttrup
  • 3,755
  • 2
  • 22
  • 36
0

Thanks to a helpful tip from CodeCaster about how to log all queries executed I was able to see that the correct amount was infact being added.

The problem was laying in the fact that it shouldnt have been adding answers atall! but because the question property had answers whenever i updated the question, it re-added all the answers! Thus leading to me seeing the correct number the whole way to save changes, but wrong numbers after.

Thanks muchly CC!

RhysW
  • 455
  • 1
  • 4
  • 16