Search code examples
asp.net.netentity-frameworkpomelo-entityframeworkcore-mysql

Double Entries in Async-Db-Requests


The following Code gives 2 Entries, but at least one is expected.

 /// <inheritdoc />
  public async Task<T> GetAsync<T>(Guid id, CancellationToken cancellationToken)
     where T : class
  {
     var entity = await Set<T>().FindAsync(new[] { id }, cancellationToken);

     return entity ?? throw new NotFoundException(typeof(T).Name, id);
  }

The given Entity is signed with a "single", so only one Entity is allowed to get back from the DB.

Steps to reproduce

call GetAsync(model.Id, cancellationToken) to receive at least one data. The Database Table contains only one Row, which should be returned.

Further technical details

EF Core version: 3.0 Database provider: Pomelo.EntityFrameworkCore.MySql Target framework: .Net Core 3.0 Operating system: Mac OS IDE: Rider


Solution

  • You need to use the DbSet.FindAsync(...) method.

    In your case the cancellationToken was interpreted as the second item of the params object[] keyValues parameter.