TheKingPinMirza
TheKingPinMirza

Reputation: 8922

Issue with _context.SaveChanges() method

I am facing strange issue.

If I remove _context.SaveChanges() from addUpdateStudentDetails() method than nothing is saved whereas i am expecting to be saved since i do have _context.SaveChanges() statement in my calling method i.e. update().

Right? Or there is soemthing else reason which is causing this?

However, if i keep _context.SaveChanges() in my called method than added/ or modified information saved successfully in database.

public void update(StudentReport report)
{
    addUpdateStudentDetails(report);
    _context.Entry(original).CurrentValues.SetValues(report);
    _context.SaveChanges();
}

private void addUpdateStudentDetails(StudentReport report)
{
    using (var context = new DBContext())
    {
        if (student != null)
            context.Entry(orignal).State = EntityState.Modified;
        else 
            context.Student.Add(orignal);
        context.SaveChanges();
    }
}

Upvotes: 0

Views: 118

Answers (1)

Zoran Horvat
Zoran Horvat

Reputation: 11301

addUpdateStudentDetails has disposed the context. Call to SaveChanges is executed on the field, not on that same context variable.

Upvotes: 3

Related Questions