-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accept Action<SetPropertyCalls>
in ExecuteUpdate
#32018
Comments
@aradalvand it's possible to build the expression tree dynamically; I agree that this is less trivial than if we we had a simple Action parameter, but adding one now would probably be problematic in terms of ambiguous resolution etc. |
@ajcvickers Why close the issue?! This could still be done by having |
Given that the current ExecuteUpdate accepts a parameter of type |
@roji I actually meant replacing the current signature with one that accepts But let me know if I'm missing something. |
That would break any code already constructing expression trees dynamically and passing them in. |
It's a breaking change nevertheless for those people already doing it, though. I'll reopen the issue to see what the team feels about it. |
Sure, but again, rest assured those people would be happy to see what they're currently doing on their own become natively supported. So this would be the best kind of breaking change to experience, if you will. EF doesn't have a strict no-breaking-changes policy and it does introduce breaking changes where it makes sense (which is a good thing IMO), and I would say this is one of those cases. But of course you guys ultimately know best. |
Note: this would also allow evaluating the value argument as a regular .NET value, rather than attempting to translate it. For example, the following fails for SQLite since SQLite does not translate DateTimeOffset.UtcNow (#32252): await ctx.Blogs.ExecuteUpdateAsync(setter => setter.SetProperty(e => e.DateTimeOffset, DateTimeOffset.UtcNow)); If ExecuteUpdateAsync accepted a non-Expression list of setters, DateTimeOffset.UtcNow would be evaluated client-side, just as if it were being set on the entity using change-tracking SaveChanges. This seems to be better than the current way of doing things (the overload accepting a lambda for the value would still be there, and would always be translated to SQL). |
Another thing that this would enable would be to pass an Expression<Func<User, int>> p = property => property.ClientId;
DatabaseContext.Entity<User>()
.Where(w => w.Id == 11)
.ExecuteUpdateAsync(e => e.SetProperty(p, 30)); That's quite a few legitimate usage types that depend on us making this change... |
Now that this is being more seriously considered, I'd like to point out that since we're now talking about "replacing" the current signature, replacing it with With db.Products.ExecuteUpdate(b =>
{
if (someCondition)
b.SetProperty(p => p.SomeProperty, "NewValue");
}); With db.Products.ExecuteUpdate(b =>
{
if (someCondition)
b.SetProperty(p => p.SomeProperty, "NewValue");
return b;
}); |
One downside of doing this, is that when one uses the SetProperty overload without the lambda (e.g. |
Another request for dynamic ExecuteUpdate: #33626. For a sample code sample of dynamic ExecuteUpdate construction with the current mechanism (quite complex!), see #33626 (comment). |
Design decision: we're OK with making this breaking change. However, we think it's quite late to do it (as a breaking change) for 9.0 - we'll try to prioritize this for the beginning of 10. |
Another subtle breaking change aspect of this: |
I was expecting to find an existing issue for this but I couldn't (if there is one please let me know):
ExecuteUpdate
currently only accepts an expression, this means you cannot dynamically build an update query, and do things like this:But this would be extremely useful and is sometimes very much needed.
IIRC I believe somebody brought this up somewhere in one of the associated issues (probably in #795), but I don't remember what the response from the EF team was. It kind of seems surprising that this isn't already supported (there are Stack Overflow questions about it, for example). The expressions passed to
.SetProperty()
are all that matters anyway, so it seems like theExecuteUpdate
parameter being an expression is unnecessary, it could've just been anAction
, but definitely correct me if I'm wrong. Thanks.Update: Found the comment that had mentioned this: #795 (comment) (see surrounding conversation) — Shay then said this:
I don't think there were any updates afterwards.\
The text was updated successfully, but these errors were encountered: