The documentation provides a good example of how to handle concurrent UPDATE scenarios, but not concurrent INSERTs. A failed insert results in a more generic DbUpdateException with no Entities provided to correct.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
I would also like to see an example in the docs for handling concurrent INSERTs. Our solution to this is to catch the DbUpdateException, then do the following check: (dbUpdateException.InnerException as SqlException)?.Number == 2601.
Is this not crazy to you all? The only way to deal with this issue is to "catch an exception". Literally that is the standard solution?
There is no concurrency issue with Inserts. Only duplicate key issues.
There is concurrency issue with inserts, one that causes duplicate key issues. You may call them by a different name but they are still are problems caused by concurrent saves by different users. EF Core should provide a way to handle this issues. Unfortunately the duplicated key exceptions don't provide info about the entries with conflict as they do in the DbUpdateConcurrencyException, that would help to implement this concurrency control in inserts. They do have plan to add it but apparently it have been pushed multiple times: https://github.com/dotnet/efcore/issues/7829
I had this requirment for one of my projects so I had to implement it using the ChangeTracker.Entries and insert them once each time until I find the entry that causes the duplicated key exception and then resolve de problem. It's a kind of ugly solution but it works... If anyone is interested you can check it here: https://github.com/vixark/SimpleOps/blob/master/SimpleOps/Datos/Contexto.cs in GuardarCambios() method.
Most helpful comment
I would also like to see an example in the docs for handling concurrent INSERTs. Our solution to this is to catch the
DbUpdateException, then do the following check:(dbUpdateException.InnerException as SqlException)?.Number == 2601.