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();
}
}
精彩评论