开发者

Updating only few fields of object

I try to update my o开发者_StackOverflow社区bjectt 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜