1

I have a student who take assessments (1..n) which are made up of tests (1..n) and thus a student takes many tests (also 1..n). When I am posting the assessment to the web service I am using a viewmodel which has the student object, (already in the db) attached. I also create a couple of tests which are sent in with the assessment. I can see from the debugger that when the assessment arrives at my web service's postassessment method, that it has everything attached including the student. However, I am getting a 409 duplicate conflict - the debugger says it is a primary key violation cannot enter duplicate. For example, if I give the student a new Guid, then everything works, relationships are created etc, but obviously this creates a new student object. I want to target the existing student and create the relationships. From reading other questions on SO, (like here and here) and this article on MSDN, I think it has something to do with the current context not knowing about the existing object and it tried to create it? I have tried to get the existing student from the db and set the assessment's student to it, attach it, etc but I am not really understanding what is happening here. Speaking of duplicates, I understand there are various versions of this question on here, but like I said, I am struggling to understand.

 public async Task<IHttpActionResult> PostAssessment(Assessment item)
    {
        using (var context = new DBContext())
        {
            Student theStudent = context.Students.Single(s => s.Id == item.Student.Id);
            item.Student = theStudent;
            //context.Students.Attach(theStudent);
            context.Entry(theStudent).State = EntityState.Modified;
            context.SaveChanges();
        }
        Assessment current = await InsertAsync(item);
        return CreatedAtRoute("Tables", new { id = current.Id }, current);  
    }

This is wrong.

1 Answer 1

1

From your code it is not clear how the other side of the relationship looks but I would think it would look something like (on the student object): theStudent.Assessments? In that case I would rather add the assessment to the student instead of adding the student to the assessment. e.g:

public async Task<IHttpActionResult> PostAssessment(Assessment item)
{
    using (var context = new DBContext())
    {
        Student theStudent = context.Students.Single(s => s.Id == item.Student.Id);
        item.Student = null;//Just to make sure there is not other relationship here
        //because 'theStudent' was retreived from the db it will be in the change graph so any changes will be recoreded
        theStudent.Assessments.add(item);//I am assuming it should be added because it is a POST method   
        context.SaveChanges();
    }
    Assessment current = await InsertAsync(item);
    return CreatedAtRoute("Tables", new { id = current.Id }, current);  
}

The other option you have it to just insert the assignments and just maintain the StudentId on the assignment (if the model was set up correctly the Assessment object should have a StudentId property as well as a Student Property) e.g:

public async Task<IHttpActionResult> PostAssessment(Assessment item)
{
    using (var context = new DBContext())
    {
        Student theStudent = context.Students.Single(s => s.Id == item.Student.Id);
        item.StudentId = item.Student.Id;
        item.Student = null;   
        //context.Students.Attach(theStudent);
        context.Assemssments.Add(item);
        context.SaveChanges();
    }
    Assessment current = await InsertAsync(item);
    return CreatedAtRoute("Tables", new { id = current.Id }, current);  
}

Hope this is what yoy were looking for.

1
  • Hi, thanks for your help. This didn't quite solve the context problem for me, but the logic of removing the student from the assessment and replacing it with the existing object and adding the assessment to the student rather than vice versa worked. As I understand it the solution, as it stands above, still causes a problem as there are two different contexts in operation. Thanks for your help.
    – Inkers
    Oct 4, 2016 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.