I only want to update a field based on the condition that is mentionned below. I know how to write it in SQL. I'm not sure how to accomplish this in entity framework.
UPDATE Table SET SomeDateTime = @NewDateTime WHERE Id = @MyId AND SomeDateTime > @NewDateTime
I want to use this particular query due to using a micro service architecture.
If you want use sql directly you can use ExecuteSqlCommand
If you were handling a object and then doing a update I would change a object and call SaveChanges
, but that's not the case.. here is an update directly to the table, If that table has millions of rows you want perform sql
to get performance on that.
example
using(var context = new SampleContext())
{
var commandText = "UPDATE Table SET SomeDateTime = @NewDateTime WHERE Id = @MyId AND SomeDateTime > @NewDateTime";
var newDateTime = new SqlParameter("@NewDateTime", myDateValue);
var myId = new SqlParameter("@MyId", myIdValue);
context.Database.ExecuteSqlCommand(commandText, new[]{newDateTime,myId});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With