entity framework - Edit model data after EF6 add -
usually tipical ef6 add should be
var newstudent = new student(); newstudent.studentname = "bill"; context.students.add(newstudent); context.savechanges();
in case i'm using logic
var student = context.students.find(id); if (student == null) { student = new student(); context.students.add(student ); } student.blabla1 = "..."; student.blabla2 = "..."; //other 20 blabla... student.studentname = "bill"; // studentname required field context.savechanges();
it's bad practise edit data model after add method on entity framework 6? context injected can thrown error on case savechanges called on method , actual thread before assignment of "studentname"?
why can't ...
var student = context.students.find(id); if (student == null) { student = new student(); modifyblabla(student );//call private method context.students.add(student); } else { modifyblabla(student );//call private method } context.savechanges();
method edit :
private void modifyblabla(student student) { student.blabla1 = "..."; student.blabla2 = "..."; //other 20 blabla... student.studentname = "bill"; }
Comments
Post a Comment