1

I want to do this in order to have a predefined list of properties to update that I can add onto. For instance, when i want to update a property of some entities, i want to update the specific property AND a predefined list of audit properties (like a datetime column called UpdatedOn)

So, for example, I want to make updates to entities using the following code:

currentContext.myEntities
.Where( some logic )
.ExecuteUpdate(e => myFunction(e)
.SetProperty( additional set property statements));

where an example of the function is something like:

public static Microsoft.EntityFrameworkCore.Query.SetPropertyCalls<T> myFunction<T>(Microsoft.EntityFrameworkCore.Query.SetPropertyCalls<T> call,DateTime timestamp)
            where T:AuditableEntity
{
     //return a setPropertyCall with one or more SetProperty statements attached
     return call.SetProperty(e => e.UpdatedOn, e=>timestamp);
}

Is this possible, is there something im missing? When i try this, i get an error at runtime that says ExecuteUpdate can only have setProperty calls inside it (which i assume means i can't have that function i list above)

If Function might not be the right approach, is there a way to programmatically create a SetPropertyCalls chain?

For Specificity, i get the error:

could not be translated. Additional information: The 'setPropertyCalls' argument to 'ExecuteUpdate' may only contain a chain of 'SetProperty' expressing the properties to be updated.

2
  • An alternative I can accept is something that builds the SetPropertyCalls object before hand so i can use that prebuilt SetPropertyCall in ExecuteUpdate Commented May 3, 2023 at 19:16
  • Suggest not adding comments but perhaps "notes" in the question to enhance clarity; and can format better etc. Commented May 3, 2023 at 19:49

1 Answer 1

0

You could use interceptors added in EF Core 7 which was release last November 2022: https://devblogs.microsoft.com/dotnet/announcing-ef7-preview7-entity-framework/

Or you can chain the SetProperty:

.ExecuteUpdate(p => p.SetProperty(x => x.LastName, x => "Updated" + x.LastName).SetProperty(x => x.FirstName, x => "Updated" + x.FirstName));

Or if it needs to be conditional you can create an expression tree: https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/expression-trees/

How to conditionally SetProperty with Entity Framework Core ExecuteUpdate?

Sign up to request clarification or add additional context in comments.

3 Comments

Does this work with ExecuteUpdate, i was under the impression that ExecuteUpdate goes around normal context things
I can't find a way to implement an interceptor for ExecuteUpdate
@DanielGoldberg I have updated my answer with a couple more options. The last one I think is what you are looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.