title: Is EnableRetryOnFailure() valid way for solving database deadlocks? Does it have negative influence on performance?
body: <p>So i encountered problem with deadlocks, i was getting this exception:</p>
  <p><code>System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseSqlServer' call.</code></p>
  <p>I even made second project to write the simplest code i can that results in the same issue, its just a few lines of code. It is really basic, you just need to have two entities that reference the same diffrent entity. It is such a common thing in my opinion and EF Core can't handle this by default and hints to use <code>EnableRetryOnFailure()</code>.</p>
  <p>I think it is quite common to have two or more entities that has relation with the same entity. We could even have something like logging system in our application that would add record to log table in database whenever someone adds data and data would be always related to some log record. Or we could save which user added some data etc. possibilities are endless.</p>
  <p><strong>My question is:</strong> If we have big application, then 1000 users could trigger retry every second, depending on how frequently they use some action that results in this deadlock. <strong>Doesn't this hurt performance? Won't this clog database eventually?</strong></p>
  <h2>Simple code to recreate this issue:</h2>
  <p>It's just API with all the code inside controllers for simplicity. Database is generated by EF Core (code first aproach). It has two controllers with endpoints that accept json file with data that is added to database. Information about file is saved everytime data from file is added to database (file name, date and time). Posting one file results in many records with data (depending on what is inside) and one record with file name, date and time (every data record gets the same file reference so all records will have the same InputFileId).</p>
  <p>You just need to send requests to both endpoints at the same time. Or just send for example 4 requests at the same time to the same endpoint. I had 2000 entries in json file so it would take a little bit longer to easier trigger deadlock.</p>
  <p><strong>User Controller:</strong></p>
  <pre><code>[Route(&quot;api/[controller]&quot;)]
  [ApiController]
  public class UsersController : ControllerBase
  {
      public MyDbContext DbContext { get; }
      public UsersController(MyDbContext dbContext)
      {
          DbContext = dbContext;
      }
  
      [HttpPost]
      public async Task&lt;IActionResult&gt; Import([FromForm]IFormFile file)
      {
          using var streamReader = new StreamReader(file.OpenReadStream());
          JsonSerializer serializer = new JsonSerializer();
  
          List&lt;User&gt; users = (List&lt;User&gt;)serializer.Deserialize(streamReader, typeof(List&lt;User&gt;));
          var inputFile = new InputFile
          {
              FileName = file.FileName,
              DateAdded = DateTime.Now
          };
  
          foreach (var user in users)
          {
              user.InputFile = inputFile;
          }
  
          await DbContext.AddRangeAsync(users);
          await DbContext.SaveChangesAsync();
  
          return Ok();
      }
  }
  </code></pre>
  <p><strong>Product Controller:</strong></p>
  <pre><code>[Route(&quot;api/[controller]&quot;)]
  [ApiController]
  public class ProductsController : ControllerBase
  {
      public MyDbContext DbContext { get; }
      public ProductsController(MyDbContext dbContext)
      {
          DbContext = dbContext;
      }
  
      [HttpPost]
      public async Task&lt;IActionResult&gt; Import([FromForm]IFormFile file)
      {
          using var streamReader = new StreamReader(file.OpenReadStream());
          JsonSerializer serializer = new JsonSerializer();
  
          List&lt;Product&gt; products = (List&lt;Product&gt;)serializer.Deserialize(streamReader, typeof(List&lt;Product&gt;));
          var inputFile = new InputFile
          {
              FileName = file.FileName,
              DateAdded = DateTime.Now
          };
  
          foreach (var product in products)
          {
              product.InputFile = inputFile;
          }
  
          await DbContext.AddRangeAsync(products);
          await DbContext.SaveChangesAsync();
  
          return Ok();
      }
  }
  </code></pre>
  <p><strong>Db context:</strong></p>
  <pre><code>public class MyDbContext : DbContext
  {
      public DbSet&lt;Product&gt; Products { get; set; }
      public DbSet&lt;User&gt; Users { get; set; }
      public DbSet&lt;InputFile&gt; InputFiles { get; set; }
  
      public MyDbContext(DbContextOptions&lt;MyDbContext&gt; options) : base(options)
      {
      }
  }
  </code></pre>
  <p><strong>Product:</strong></p>
  <pre><code>public class Product
  {
      public long Id { get; set; }
      public string SerialNumber { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public long InputFileId { get; set; }
      public InputFile InputFile { get; set; }
  }
  </code></pre>
  <p><strong>User:</strong></p>
  <pre><code>public class User
  {
      public long Id { get; set; }
      public string Name { get; set; }
      public string Surname { get; set; }
      public long InputFileId { get; set; }
      public InputFile InputFile { get; set; }
  }
  </code></pre>
  <p><strong>InputFile:</strong></p>
  <pre><code>public class InputFile
  {
      public long Id { get; set; }
      public string FileName { get; set; }
      public DateTime DateAdded { get; set; }
  }
  </code></pre>
  <p><strong>Startup.cs, ConfigureServices:</strong></p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddControllers();
      services.AddDbContext&lt;MyDbContext&gt;(options =&gt; options.UseSqlServer(Configuration.GetConnectionString(&quot;DefaultConnection&quot;)));
  }
  </code></pre>