Search code examples
c#entity-frameworknullableef-fluent-apief-core-5.0

Nullable warning with EF Core fluent API


I have nullable enabled in VS2022.

I don't see what the valid resolution to this nullable warning is.

builder.OwnsOne(o => o.CreatedByUser, createdByUser => {
createdByUser.Property(userRef => userRef.UserId)
  .HasColumnName("CreatedByUser");
});

enter image description here

Changing

userRef.UserId

to

userRef?.UserId

would clear the error, but it's a fluent API definition, and that would make no sense (I believe).

I could add warning suppression, but that's equally rubbish.

What would be an appropriate resolution to this warning, please?


Solution

  • Many fluent (and other) APIs in pre EFC 6.0 are not NRT annotated, hence you have to suppress NRT warnings or use null forgiving tricks.

    This has been fixed in EFC 6.0, Reference: What's New in EF Core 6.0 - EF Core annotated for C# nullable reference types:

    GitHub Issue: #19007.

    The EF Core codebase now uses C# nullable reference types (NRTs) throughout. This means that you will get the correct compiler indications for null usage when using EF Core 6.0 from your own code.