Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a record using Entity Framework? [duplicate]

How can I convert this code to Entity Framework?

update cachieroperation set last_used = getdate()+'0:8:0' where id = 14
like image 801
adilet.571 Avatar asked Aug 05 '26 00:08

adilet.571


2 Answers

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();
     }
}
like image 140
Florian Schmidinger Avatar answered Aug 06 '26 13:08

Florian Schmidinger


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

like image 44
Amit Bisht Avatar answered Aug 06 '26 13:08

Amit Bisht



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!