2

How can I convert this code to Entity Framework?

update cachieroperation set last_used = getdate()+'0:8:0' where id = 14
0

2 Answers 2

9

Should be something like(I don't know what your classes are called):

using(var context = new SomeEntities())
{
     CarrierOperation carrierOperation = context.CarrierOperations.SingleOrDefault(co=> co.id == 14);
     if(carrierOperation != null)
     {
         carrierOperation.last_used = DateTime.Now.AddMinutes(8);
         context.SaveChanges();
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This answer execute a SELECT, loads all data of record (transfer from DB to application) then executes the update. I have suggested a duplicate on the question to use object model but only execute the UPDATE
1

You can use ExecuteQuery for executing queries directly against the database:

string query = "update cachieroperation set last_used = getdate()+'0:8:0' where id = 14";
context.ExecuteQuery<cachieroperation>(query );  

For more info, see Microsoft Docs on ExecuteQuery

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.