'EF Core executing previously failed query on SaveChanges

We have a .Net Core 3.1 application which is using EF Core to connect wit the SQL Server database. We are facing one problem, that when an exception occurs in our method due to some reason, for example, a field is mandatory in DB and on SaveChanges, Exception is raised, because the value for that field was passed as null, then next time, when the same method is called with different parameters (even with all correct/mandatory fields), still the old query gets executed in EF Core (I checked this in Output window). Which is very weird and strange behavior.

If we close the application (in debug environment) after the first exception and re run the application with correct payload next time, then everything works fine. May be the EF Core is retrying the earlier failed query again for some reason? or why is this behavior occurring?



Solution 1:[1]

Depending on how are you getting messages from the queue, the message may come back to the queue if it is not acknowledge after a while.

With Auto Acknowledge the message will is removed permanently. If you ack after persist in the database, any exception will avoid the ack, so the message goes back to the queue.

Solution 2:[2]

I just figured out that the RabbitMQ consumer classes were not properly setup in the Dependency Configuration file. Basically the repository cases were instantiated as static members in the Dependency Config and when Consumers were setup for RabbitMQ, they were passed in these static Repository objects. I removed these static objects, and made use of the Scoped instances of the repository. this resolved my issue.

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 WilliamK
Solution 2 WAQ