'Update entity without retrieving first, when the Id is not known but another unique field is
If I have an entity called Book with an Id, Title and Price, how can I update that record's Price without retrieving it from the database first? I only know the Title at the time of the update.
Title is guaranteed to be unique in this contrived example.
Normally, I could use Attach(), but that requires me to know the Id, which I don't have.
Basically I want this statement to come out of EF:
update Book set Price = "1.99" where Title = "Blah";
Thanks
Solution 1:[1]
YOU can call it Bach update or delete action in EF.
Here the same code for this work. please see it
Using this library, Entity Framework Core users can delete or update multiple records from a LINQ Query in a SQL statement without loading entities. This library supports Entity Framework Core 5.0 and Entity Framework Core 6.0.
await ctx.BatchUpdate<Book>()
.Set(b => b.Price, b => b.Price + 3)
.Set(b => b.Title, b => s)
.Set(b => b.AuthorName,b=>b.Title.Substring(3,2)+b.AuthorName.ToUpper())
.Set(b => b.PubTime, DateTime.Now)
.Where(b => b.Id > n || b.AuthorName.StartsWith("Zack"))
.ExecuteAsync();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
