Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Add filtering to UPDATE/DELETE EF generated queries #6

Open
yar-shukan opened this issue Jun 16, 2014 · 2 comments
Open

Add filtering to UPDATE/DELETE EF generated queries #6

yar-shukan opened this issue Jun 16, 2014 · 2 comments

Comments

@yar-shukan
Copy link

In order to make one call to the db and filter the updated set with custom filter it would be great to make filters work with UPDATE/DELETE EF generated queries.
When I make something like:

DbEntityEntry<T> dbEntityEntry = _context.Entry(updatedEntity);
dbEntityEntry.State = EntityState.Modified;
_context.SaveChanges();

By default EF creates:

UPDATE [dbo].[EntityTable]
SET ....
WHERE ([Id] = @1)

It would be great if filters could inject parameters to UPDATE/DELETE like it is for SELECT queries, so when I add FilterConvention.Create and enable the filter, it's injected in UPDATE/DELETE where clause.

@jbogard
Copy link
Owner

jbogard commented Jun 23, 2014

Why would you also want this filter on update/delete queries?

@yar-shukan
Copy link
Author

By default EF uses PK fields in WHERE clause in UPDATE/DELETE. For instance, I have Address table with AddressId as PK, UserId as FK to User's table record and some other fields. To update or delete address from API, let's say, caller must provide AddressId. So to force security rule "user can UPDATE/DELETE what belongs to him (records with his UserId)" I have 2 options here:

  1. Make a call to retrieve the Address by UserId and AddressId. If retrieved object is not null, then we can update/delete it, so 2 calls are needed, which is not elegant solution.
  2. Use API of https://github.com/loresoft/EntityFramework.Extended , but it makes call to the db when calling .Update(expression) or .Delete(expression), I don't know why it was implemented this way, maybe because of EF design limits, but I don't like this idea of making Update/Delete calls before making .SaveChanges().
    That's why I thought it would be a great feature to add to the EF.Filters functionality in order to make filtering of what can be updated/deleted when making UPDATE/DELETE with EF.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants