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
Feat: Conditional insert #578
Comments
Hello @tsanton , Here are a few questions to better understand your requirement
Let me know more about your scenario. Depending on your answer, we might already be very close to supporting it. Best Regards, Jon |
Hi @JonathanMagnan! I'm on Postgres, yes. I'm already running this pattern for updates and deletes. Here is how I implement a conditional delete of "SomeEntity" based on the ValidFrom queryable predicate var query = from ua in context.SomeRandomEntity
let minValidFrom = (
from x in context.SomeRandomEntity
where x.TenantId == tenantId && x.SomeId == pred.SomeId
select x.ValidFrom
).Min()
where ua.TenantId == tenantId && ua.SomeId == pred.SomeId && ua.Id == pred.Id && ua.ValidFrom > minValidFrom
select ua;
var strategy = context.Database.CreateExecutionStrategy();
return await strategy.ExecuteAsync(async () =>
{
await using var transaction = await context.Database.BeginTransactionAsync(ct);
var deleted = await query.ExecuteDeleteAsync(ct);
if (deleted != 1)
{
await transaction.RollbackAsync(ct);
return false;
}
await transaction.CommitAsync(ct);
return true;
}); |
Thank you for the additional information. We will work on it. Best Regards, Jon |
One of the, in my option, larger limitations with EF at the moment is the lack for conditional singleton inserts.
My current case is as follows: I'm allowing users to manipulate a history table, but with certain limitations.
For instance I will allow them to create a new statuses, but that status can't be backdated with
"valid_from" <= min(valid_from) where entity was created (status == 'created')
.As of now I have to look up the entity (or run an .Any() with a predicate), and then insert if it passes the predicate, whereas I'd much rather just fire off
IQueryable.Where(prediates).ConditionalInsertAsync(Entity)
and return the count from the output to see if one went in or if 0 inserted (and then return conditional responses based on the feedback).In terms of design (at least for Postgres) I'm thinking something along these lines:
I'm posting the suggestion here firstly because I think a lot of the required pipework for this extension already exist within the existing code base. Further I think it's a killer extension that I'm somewhat perplex that I can't find an implementation for -> it surely would save a lot of time and boilerplate code.
I can also say that though it's on the EF core radar (here) I would not put money on it making the EF core 9 cut. Nor is it completely clear to me if the design supports the conditional bit.
Hoping to hear back from you and I'd be happy to help with other SQL-provider syntax research or whatever you feel you might need in order to get this into either extension or plus!
/T
The text was updated successfully, but these errors were encountered: