'Field not found: 'Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.ThrowReadValueExceptionMethod'

I am trying to make Integration tests of my WebApi controllers through NUnit but I am getting this error:

Field not found: 'Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.ThrowReadValueExceptionMethod'.

The error occurs exactly when I call this line from Entity Framework:

await base.SaveChangesAsync(true, cancellationToken);

The 'interesting' part is that I am not having this error when I test the api directly (in localhost), only when I test it from Nunit.

This is the code from my test class:

[Test]
public async Task PutAusschreibungen_WhenCalled_ReturnAddedAusschreibendeStellen()
{
   Guid id = Guid.NewGuid();
   AusschreibungDto ausschreibungDto = new AusschreibungDto
   {
      Id = id,
      Nr = 0,
      Status = "Test",
      Quelle = new QuelleDto
      {
          Id =  Guid.NewGuid(),
          Name = "QuelleTest",
          EingangTyp = EingangTyp.Email
      }
   };
   IActionResult result = await _ausschreibungenController.PostAusschreibung(It.IsAny<string>(), ausschreibungDto);
   Assert.That(result, Is.TypeOf<CreatedAtActionResult>());

}

This one is the code from my Controller:

        [HttpPost]
        public async Task<IActionResult> PostAusschreibung(string dbType, [FromBody] AusschreibungDto ausschreibungDto)
        {
            //if (!ModelState.IsValid)
            //{
            //    return new BadRequestObjectResult(ModelState);
            //}
            //Not neccesary because ControllerBase makes Validation automatically

            using (IUnitOfWork uow = _uowF(dbType))
            {
                Ausschreibung ausschreibung = _mapper.Map<AusschreibungDto, Ausschreibung>(ausschreibungDto);
                uow.Ausschreibungen.Add(ausschreibung);

                try
                {
                    await uow.CompleteAsync();
                }
                catch (DbUpdateException ex)
                {

                    return StatusCode(500, ex.ToString());
                }

                AusschreibungDto result = _mapper.Map<Ausschreibung, AusschreibungDto>(ausschreibung);
                return CreatedAtAction("GetAusschreibung", new {dbType, nr = result.Nr},
                    "Der Ausschreibung-Einträge wurde erfolgreich in die Datenbank aufgenommen.");
            }
        }

I am using AspNet Core 2.1.0 and Entity Framework Core 2.1.1

Any help would be appreciated!



Solution 1:[1]

I had to upgrade the EF to version 5 or higher to resolve this issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mohammedn