dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.77k stars 3.18k forks source link

Interceptor support for bulk updates #30869

Open gediminas-eldorado opened 1 year ago

gediminas-eldorado commented 1 year ago

In our app, we have some rules, which we want applied always, when a save to database happens.

For example: set modified date.

Previously, this could easily be achieved with interceptors.

Now, with bulk updates, it seems that this logic needs to be included manually to each bulk update command.

Question: Is there a way to intercept bulk update operations currently? If not, is there a possibility for this in the future?

Thanks.

roji commented 1 year ago

This is something that should be possible via the new LINQ expression tree interceptor, though it's probably not trivial to do.

gediminas-eldorado commented 1 year ago

We were also thinking that this could be achieved via computed columns, but a solution not resorting to raw sql queries does seem cleaner.

rjperes commented 4 months ago

I implemented something similar for bulk deletes, to implement soft-delete functionality. Might be useful to others, this requirement (e.g., set audit fields) would be very easy to implement, actually: https://asp-blogs.azurewebsites.net/ricardoperes/soft-deletes-with-entity-framework-core-bulk-deletes