EF Core reference/clean up issue

McAninch, Robin 51 Reputation points
2022-07-15T14:52:12.447+00:00

I have a .net core 3.1 service that is running and inserting a row into a SQL Server table using Entity Framework Core version 5.0.11. The stripped down code looks like this

public async Task<int> InsertRow( string str1, string str2 ....)
{
int result = 0
logging and try catch info here

**using (dbContext context = new dbContext(ConnectionString))**  
{  
      context.TEvent.Add(new TInsertedTable()  
      {  
                           add properties  
     });  

    **result = await context.SaveChangesAsync();**  

                    
}  

catch logic here

return result

            }  

this method is consumed by the main process flow at the end

counter = await DB.InsertRow(str1, str2, str3 ...);

which rolls up to the ExecuteAsync method in the Worker and so on.

Issue
It writes to the db fine. One of the columns is a a processing flag for the next system to pick it up and do something with it. That service keeps performing its task over and over again until we stop the service at which point the column seems to get updated by the downstream service and the infinite loop stops. It seems like EF is not releasing the insert to be updated after it writes it. I tried to do an insert with the processing flag set to false, then do an update to set it to true and it updates but keeps the "hold".

If I use a scheduled task/console app and let it run and stop there is no issue. I could also fix this with a stored proc call which is what another developer did to get around one rogue call in EF that didn't seem to work but I would like to understand better what I am doing wrong. The await/async seems correct and is implemented all the from end to end, other insertion calls seem to work find in the application to different tables it is just this one.

Any ideas would be appreciated. I have tried removing the using in favour of a try/catch/finally, tried to explicitly call garbage collection as well but the other system seems to keep writing until I stop the service

thank you very much

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
698 questions
{count} votes