ARTICLE AD BOX
I am using the Bulk Update APIs, part of EF Core like the ExecuteUpdateAsync() functions, to perform updates, and I am passing in a lambda for my valueExpression. I am wondering if the evaluation of the lambda expression happens in memory, or on the DB Side?
My code has a call the the ExecuteUpdateAsync() function as follows, and the dto variable is an input parameter during function call.
public async Task<T> MyFunc(CustomType dto) { // some existing code... if (await Data.MyTable .Where(e => e.id == dto.id) .ExecuteUpdateAsync(e => e .SetProperty(p => p.Value, p => dto.Value ?? p.Value)) != 1) throw; // some existing code... }For all of the below, assume that dto.Value is not null (if it was null, instead of passing @__dto_Value_1, it directly gets evaluated to null)
I tried to see the generated SQL command, but it seems like the valueExpression is evaluated before generating the SQL, resulting in the UPDATE command not having a call to the COALESCE() operator (i.e., something like UPDATE "MyTable" SET "Value" = <result of the valueExpression evaluation> WHERE MyTable."Id" == @__dto_Id_1).
If I instead passed something as follows in my SetProperty() function call:
public async Task<T> MyFunc(CustomType dto) { // some existing code... if (await Data.MyTable .Where(e => e.id == dto.id) .ExecuteUpdateAsync(e => e .SetProperty(p => p.Value, p => p.Value ?? dto.Value)) != 1) throw; // some existing code... }the generated SQL statement seems be of the format,
UPDATE "MyTable" SET "Value" = COALESCE(MyTable."Value", @__dto_Value_1 WHERE MyTable."Id" == @__dto_Id_1Does this mean that if the left-hand part of the null-coalescing check is present in C# memory, the evaluation is done in-memory, but if it depends on DB side values, it will use the COALESCE() operator and pass off the null checks to the DB?
