Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Resolve Error: Required properties '{'Prop1', 'Prop2'}' are missing for the instance of entity type 'SomeType''?

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();
}
like image 610
derekbaker783 Avatar asked Nov 26 '25 12:11

derekbaker783


2 Answers

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));
}
like image 139
derekbaker783 Avatar answered Nov 29 '25 03:11

derekbaker783


If you don't want to set a empty string on every prop, you can explicitly set then as nullable:

 public string? Prop1 { get; set; }
 public string? Prop2 { get; set; }

I used it and solved my problem.

like image 33
Leonardo Goltara Avatar answered Nov 29 '25 04:11

Leonardo Goltara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!