-
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
How to perform UPDATE query with multiple conditions? #30609
Comments
@verdysh SaveChanges, always sends updates based on the primary key. If Id is your primary key above, then it already uniquely identifies the row (and the additional TenantId WHERE clause wouldn't do anything). Otherwise, you may be looking to have a composite primary key consisting of both the Id and the TenantId. If you configure your entity type that way, SaveChanges should automatically reference both in the WHERE clause. |
I have a request sent from the client. Client sends following object:
Client's object doesn't have To update the entity I have to check if If I could say |
In that case, you can define TenantId as an application-managed concurrency token. This means that it gets included in SaveChanges-generated WHERE clauses, and if the UPDATE fails to find a matching row, an exception is thrown (which you can catch and ignore). |
It works, thanks! But 🙂 ... I have different cases where I need to update the entity in different ways. The case I described above is "unsafe" because data comes from the client and I must check if the user belongs the same tenant as the row. Also there are other cases where I only have Ideally, I want to be able to say, not for all cases, but for a specific case: EF, please add an additional condition here for the UPDATE statement. Is it possible? |
Also, just to make sure the picture is complete - you can very precisely control your WHERE clause by using ExecuteUpdate. This does bypass change tracking entirely, so you may need to do more manual management, depending on what exactly you're doing. |
Hi!
Microsoft.EntityFrameworkCore.SqlServer, 6.0.5
I have following entity:
I need to update it. I want to perform the next UPDATE query:
UPDATE ContentFilters SET Name=@name WHERE Id=@id and TenantId=@tenantId
If I write following code
I get the next UPDATE query:
UPDATE [ContentFilters] SET [Name] = @p0 WHERE [Id] = @p1;
But I want EF to add another condition
and tenantId = @p2
into WHERE clause.Is it possible?
The text was updated successfully, but these errors were encountered: