添加 EF6 后编辑模型数据

Edit model data after EF6 add

通常典型的 EF6 添加应该是

var newStudent = new Student();
newStudent.StudentName = "Bill";
context.Students.Add(newStudent);
context.SaveChanges();

在我的例子中,我使用的是这个逻辑

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 is a required field
context.SaveChanges();

在 entity framework 6 上添加方法后编辑数据模型是一种不好的做法?在注入上下文的情况下,如果在另一个方法上调用 savechanges,则可能会引发错误,而我的实际线程就在 "StudentName"?

的分配之前

为什么你不能这样做...

   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();

编辑方法:

private void ModifyBlabla(Student student)
{
   student.blabla1 = "...";
   student.blabla2 = "...";
   //other 20 blabla...
   student.StudentName = "Bill";
}