18

Question: how to resolve this error:

Microsoft.EntityFrameworkCore.DbUpdateException: Required properties '{'Prop1', 'Prop2'}' are missing for the instance of entity type 'SomeEntity' with the key value '{Id: 1}'

I have a test suite that uses the EF Core in-memory provider. I recently upgraded all EF Core NuGet packages in the solution to target version 6.0.1.

When running some of the tests post-upgrade, I encountered the error mentioned above.

Both of the properties mentioned in the error are strings.

I've reviewed some Github issues and Stack Overflow posts that the search engine hit on when DuckDuckGo'ing the error message, but this didn't turn up anything interesting.

The affected class has a config method like this:

public void Configure(EntityTypeBuilder<SomeType> builder)
{
    builder
        .Property(someType => someType.Prop1)
        .IsRequired();

    builder
        .Property(someType => someType.Prop2)
        .IsRequired();
}
derekbaker783
  • 8,109
  • 4
  • 36
  • 50

1 Answers1

31

Apparently v6 of the EFCore in-memory provider enforces .IsRequired().

The solution in my case was to update the tests so that the string properties mentioned in the error message are not null.

instanceOfSomeType.Prop1 = "NotNull";
instanceOfSomeType.Prop2 = "NotNull";

Alternately, you can disable this functionality:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder
        .LogTo(Console.WriteLine, new[] { InMemoryEventId.ChangesSaved })
        .UseInMemoryDatabase("UserContextWithNullCheckingDisabled", b => b.EnableNullChecks(false));
}
derekbaker783
  • 8,109
  • 4
  • 36
  • 50