how can apply changes first record in table , add 1 or more additional records same table? getting following exception:
an object same key exists in objectstatemanager. objectstatemanager cannot track multiple objects same key
see sample code below.
var student = db.student.where(d => d.studentid == studentid); int count = 0; if(student != null) { foreach(var student in student) { if (student.id == id) { foreach (var assign in assignment) { if (assign != null && count == 0) { //how can save changes made first record here assign.assignmentid = student.assignmentid; db.assignment.applycurrentvalues(assign); } if (assign != null && count > 0) { //how can add new records here assign.assignmentid = student.assignmentid; db.assignment.addobject(assign); } count++; } } } }
try calling save after line i.e.
db.assignment.applycurrentvalues(assign); db.savechanges();
then new entries:
var assignment = new assignement() { assignmentid = student.assignmentid }; db.assignments.add(model); db.savechanges();
Comments
Post a Comment