title: Entity Framework Core Error Transaction 20205
body: <p>I'm facing an issue with Entity Framework Core 5.0.4 in a .NET5 web project.</p>
  <p>I have a <code>BaseRepository&lt;T&gt;</code> where I define all the function, for example (full implementation <a href="https://github.com/erossini/AdminLTEWithASPNETCore/blob/main/PSC.Repositories/Repositories/BaseRepository.cs" rel="nofollow noreferrer">here</a>)</p>
  <pre><code>public class BaseRepository&lt;T&gt; : IAsyncRepository&lt;T&gt; where T : class
  {
      /// &lt;summary&gt;
      /// The log
      /// &lt;/summary&gt;
      protected readonly ILogger _log;
  
      /// &lt;summary&gt;
      /// The database
      /// &lt;/summary&gt;
      protected PSCContext _db;
  
      public BaseRepository(PSCContext db, ILogger&lt;BaseRepository&lt;T&gt;&gt; log)
      {
          _db = db;
          _log = log;
      }
  
      /// &lt;summary&gt;
      /// Add a new entity as an asynchronous operation.
      /// &lt;/summary&gt;
      /// &lt;param name=&quot;entity&quot;&gt;The entity.&lt;/param&gt;
      /// &lt;returns&gt;A Task&amp;lt;T&amp;gt; representing the asynchronous operation.&lt;/returns&gt;
      public async Task&lt;T&gt; AddAsync(T entity)
      {
          _log.LogDebug($&quot;[{nameof(T)}] Adding a new record&quot;);
  
          await _db.Set&lt;T&gt;().AddAsync(entity);
          await _db.SaveChangesAsync();
  
          return entity;
      }
  }
  </code></pre>
  <p>All repositories in my project are based on this base class. I have a lot of insert in my procedure. When I have to save a new record in a repository with <code>enum</code> I have this error:</p>
  <blockquote>
  <p>fail: Microsoft.EntityFrameworkCore.Database.Transaction[20205]
  An error occurred using a transaction.</p>
  </blockquote>
  <p>How you see in the following image, I have a lot of insert before this error. In my code there is not transaction at all. The error occurs when I want to save</p>
  <p><a href="https://i.stack.imgur.com/c7PKs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c7PKs.png" alt="enter image description here" /></a></p>
  <p>The definition of the table is</p>
  <pre><code>[Table(&quot;ImportedHistoryFileLogs&quot;)]
  public class ImportedHistoryFiletLog : ITable
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public long ID { get; set; }
      public DateTime CreatedAt { get; set; } = DateTime.Now;
      public string CreatedBy { get; set; }
      public DateTime? ModifiedAt { get; set; }
      public string ModifiedBy { get; set; }
      public long ImportedHistoryFileId { get; set; }
      [ForeignKey(&quot;ImportedHistoryFileId&quot;)]
      public ImportedHistoryFile ImportedHistoryFile { get; set; }
      public DateTime LogData { get; set; } = DateTime.Now;
      public LogType LogType { get; set; } = LogType.Information;
      public LogEventType LogEventType { get; set; } = LogEventType.InternalAction;
      public string Body { get; set; }
      public string Message { get; set; }
      public string UserId { get; set; }
  }
  </code></pre>
  <p>This table has a relationship with another table <code>ImportedHistoryFile</code>. Before the error, I add in this table the same code for other records.</p>
  <p>Not always have this error. Sometimes the procedure stops without any error or message.</p>
  <p>Also, I noticed the same error in other tables. Then, I was thinking that Entity Framework Core has a number of operations (like a buffer or concurrency) and when I reach the maximum of the operations it gives me an error. How can I avoid this error?</p>
  <h2>Update</h2>
  <p>Could the issue be related to <code>SaveChangesAsync()</code>? Apparently, if I change that line with <code>_db.SaveChanges();</code> is working.</p>