Skip to content
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

Add predicate to ExecuteUpdateAsync #42

Closed
NotAsea opened this issue Dec 22, 2023 · 3 comments
Closed

Add predicate to ExecuteUpdateAsync #42

NotAsea opened this issue Dec 22, 2023 · 3 comments

Comments

@NotAsea
Copy link

NotAsea commented Dec 22, 2023

ExecuteUpdateAsync of EFCore rely on the source IQueryable has already been filtered by Where Linq, but in your version, i can not see anywhere to add filter for your repository, which mean whenever we call something like

await repository.ExecuteUpdateAsync<Person>(
    sp => sp.SetProperty(t => t.IsDeleted, true)
)
// translate to
/*
UPDATE People SET IsDeleted = true;
*/

whole record will get updated immediately, maybe you can add an predicate to your ExecuteUpdateAsync to let consumer supply their own filter condition just like what we can do in EFCore

await context.People
    .Where(p => p.CreatedDate <= DateTime.Now.AddDays(-30))
    .ExecuteUpdateAsync(sp =>  sp => sp.SetProperty(t => t.IsDeleted, true)) );
// now we have where clause
/*
UPDATE People SET IsDeleted = false
WHERE CreatedDate <= @p__linq__0;
*/
@NotAsea
Copy link
Author

NotAsea commented Dec 22, 2023

Browsing around source code i find your implementation like this

public async Task<int> ExecuteUpdateAsync<TEntity>(
                    Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls,
                    CancellationToken cancellationToken = default)
                    where TEntity : class
{
     int count = await _dbContext.Set<TEntity>().ExecuteUpdateAsync(setPropertyCalls, cancellationToken);
     return count;
}

So instead of calling ExecuteUpdateAsync directly in DbSet, you can change function a little like so

public async Task<int> ExecuteUpdateAsync<TEntity>(
                    Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls,
                    Expression<Func<TEntity, bool>>? predicate = null
                    CancellationToken cancellationToken = default)
                    where TEntity : class
{
    var dbSet = _dbContext.Set<TEntity>();
    if (predicate != null) dbSet = dbSet.Where(predicate);
    return await dbSet.ExecuteUpdateAsync(setPropertyCalls, cancellationToken);
}

Anyway, your library is already good, filter in ExecuteUpdateAsync is some niche thing

@TanvirArjel
Copy link
Owner

@NotAsea Thank you so much for reaching out. Surely I am taking care of it.

@NotAsea
Copy link
Author

NotAsea commented Dec 24, 2023

closing as @TanvirArjel has already had planned for it

@NotAsea NotAsea closed this as completed Dec 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants