Entity Framework (EF) and .Net 6 Blazor not updating database

Guillermo Perez 26 Reputation points
2021-10-24T17:52:23.527+00:00

I need help trying to understand why my Blazor component in .net Core 6 is not updating my database when removing an entity, please take a look at my code sample: (beforehand, thanks for all your help!)

First, I'm injecting my db context as follow:

@inject AppDbContext db

then, below on my code, I decided to query for some data, in this case, Customers as follow:

@code {

      private List<Customer> clients { get; set; }

    protected override async Task OnInitializedAsync()
    {
        clients = await db.Customers.ToListAsync();
    }

}

Now, I created a button with a method to delete the customer; first I search the entity, if found, then I remove the item from the "clients" list from the database, as follow:

private async Task DeleteCustomer(int CustomerId)
    {    
        Customer? customer = clients.FirstOrDefault(f => f.Id == CustomerId);

        if (customer is not null)
        {           
            clients.Remove(customer);
            await db.SaveChangesAsync();
        }

    }

THE PROBLEM is that the entity is removed from the list but not removed from the database, so when I refresh the item still there, I have to apply another command inside the if to make it work:

            db.Remove(customer);

It seems to lose completely the connection between the list coming from the DB and the database. This is the first time I see something like this, am I missing something? am I using the EF the way I am supposed to do? I can just add that command and make it work but I don't think is a good practice, please help!

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
696 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,378 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce Barker 801 Reputation points
    2021-10-25T15:39:07.253+00:00

    there is no connection between the client list

    clients = await db.Customers.ToListAsync(); 
    

    and the database context. the list is just a list. your confusion may be because objects in the list are the same objects in DbContext. so if you update a property on an object in the list, it also updates the property for the same object in the DbContext.

    but removing the object from the list, does not remove it from the DbContext.


  2. Zhi Lv - MSFT 32,006 Reputation points Microsoft Vendor
    2021-10-26T06:00:05.823+00:00

    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

    0 comments No comments