I have a method for updating some tables. For update I need get first of TestProcess
, but I don't like that. How can I update TestProcess
without select(firstOrDefault)
operation, used only for the update operation?
Example of method:
public void UpdateTestProcess(int id, string updateID)
{
using (TestEntities context = new TestEntities())
{
TestProcess pr = context.TestProcess.FirstOrDefault(x => x.MyID == id);
pr.UpdateID = updateID;
context.TestProcess.Attach(pr);
context.ObjectStateManager.ChangeObjectState(pr, EntityState.Modified);
context.SaveChanges();
}
}
If you are setting the value to the default value of that type (for example, setting an
int
to0
) it won't be picked up as a change, and you need to manually set the state.You can put such code away in extension methods, so you can do things like this (I'll leave the implementation as an exercise):
You can do like that (you probably should have all the test process data):
The code:
The result TSQL:
Note:
The "IsModified = true" line, is needed because when you create the new TestProcess object (only with the MyID property populated) all the other properties has their default values (0, null, etc). If you want to update the DB with a "default value", the change will not be detected by entity framework, and then DB will not be updated.
In example:
will not work without the line "IsModified = true", because the property UpdateID, is already null when you created the empty TestProcess object, you needs to say to EF that this column must be updated, and this is the purpose of this line.