Hi @Guillermo Perez ,
//--- remove from the list...
clients.Remove(customer);
//---then remove it from the db...
db.Remove(customer);
await db.SaveChangesAsync();
The above code and workflow are correct.
In your application, you are using the clients
list to store the query records.
After query data from database, and store the records in the clients
, the clients and the database have been completely separated.
Then, when you add/modify/delete the item in the clients it will not update the database. To update the data on clients and database, as the above code, you need to delete an item from the client and the database at the same time.
Besides, you could also delete the entity from database first, then update the clients list. Refer the following code:
private async Task DeleteCustomer(int CustomerId)
{
Customer? customer = clients.FirstOrDefault(f => f.Id == CustomerId);
if (customer is not null)
{
//---then remove it from the db...
db.Remove(customer);
await db.SaveChangesAsync()
//re-query the database and update the clients
clients = db.Customers.ToListAsync();
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion