views:

37

answers:

1

I try to update my objectt task in database, but in parameter I have object which has clean history (I don't get it from database). And my question is :

Is it the best way to update object:

 public static void SaveEditedTask(Task task)
        {
            using (var context=new Entities())
            {
                var taskToModification = context.Tasks.Where(t => t.IDTask == task.IDTask).First();
                taskToModification.IDProject = task.IDProject;

                context.SaveChanges();
            }
        }

?

I use EF4

+1  A: 

Your code requires additional query to DB. You can also use

  public static void SaveEditedTask(Task task) 
  { 
    using (var context=new Entities()) 
    { 
      context.Tasks.Attach(task);
      context.ObjectStateManager.GetObjectStateEntry(task).SetModifiedProperty(t => t.IDProject);

      context.SaveChanges(); 
    } 
  } 
Ladislav Mrnka