title: Duplicate key value Error when updating a lot of data
body: <p>When I want to save a lot of data in the database (PostgreSQL 12 and the Entity Framework Core (C#)) I get the following exceptions:</p>
  <pre><code>fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
        Failed executing DbCommand (197ms) [Parameters=[@p0='?', @p1='?', @p2='?' (DbType = DateTimeOffset), @p3='?'], CommandType='Text', CommandTimeout='30']
        INSERT INTO &quot;FileInfos&quot; (&quot;FileId&quot;, &quot;FileName&quot;, &quot;LastModifiedDateTime&quot;, &quot;Path&quot;)
        VALUES (@p0, @p1, @p2, @p3);
  fail: Microsoft.EntityFrameworkCore.Update[10000]
    An exception occurred in the database while saving changes for context type 'PostgreSQLConnect.ContextModels.WebhookContext'.
    Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
     ---&gt; Npgsql.PostgresException (0x80004005): 23505: duplicate key value violates unique constraint &quot;PK_FileInfos 
      Severity: FEHLER
        SqlState: 23505
        MessageText: double key value violates unique constraint »PK_FileInfos«
        Detail: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
        SchemaName: public
        TableName: FileInfos
        ConstraintName: PK_FileInfos
        File: d:\pginstaller_12.auto\postgres.windows-x64\src\backend\access\nbtree\nbtinsert.c
        Line: 570
        Routine: _bt_check_unique
  </code></pre>
  <p>Some data needs to be updated, some needs to be created first. This method works for some data, but if I add more than one at a time, I get the exception mentioned above.</p>
  <pre><code>    private async Task SaveFileInfos(FileInfo fileInfo)
      {
          var foundFileInfo = _context.FileInfos.
          Where(f =&gt; f.FileId == fileInfo.FileId).FirstOrDefault();
  
          if (foundFileInfo == null)
          {
              await _context.FileInfos.AddAsync(fileInfo);
          }
          else
          {
              foundFileInfo.FileName = fileInfo.FileName;
              foundFileInfo.LastModifiedDateTime = fileInfo.LastModifiedDateTime;
              foundFileInfo.Path = fileInfo.Path;
          }
  
          await _context.SaveChangesAsync();
      }
       
  </code></pre>
  <p>The entity class:</p>
  <pre><code>    public class FileInfo : IFileInfo
      {
  
      [Key]
      public string FileId {get; set;}
  
      public string FileName {get; set;}
  
      public DateTimeOffset? LastModifiedDateTime {get; set;}
  
      public string Path {get; set;}
      }
  </code></pre>
  <p>The context class:</p>
  <pre><code>   public class WebhookContext : DbContext
     {
      public WebhookContext(DbContextOptions&lt;WebhookContext&gt; options) : base(options) { }
  
      public DbSet&lt;FileInfo&gt; FileInfos { get; set; }
      }
  </code></pre>
  <p>The loop that calls the save method:</p>
  <pre><code> private async Task ConvertAndSaveFiles(IDriveItemDeltaCollectionPage files)
    { 
  
      foreach (var file in files)
      {
           await SaveFileInfos(file.Name, file.Id, file.LastModifiedDateTime,
                      file.ParentReference.Path);
      }
             
    }
  </code></pre>
  <p>BTW: The Id has already been generated by another application and is 34 characters long.</p>
  <p>What mistake did I make as a beginner?  :-)</p>