开发者

Updating only few fields of object

开发者 https://www.devze.com 2023-01-15 14:46 出处:网络
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 :

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

精彩评论

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