question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Detect non-nullable NRT properties only when both read and write are non-nullable

See original GitHub issue

For properties where read and write differs (i.e. [AllowNull]), our current NRT convention considers only read nullability; the following property is detected as non-nullable:

[AllowNull]
public string NonNullablePropertyAllowNull { get; set; }

I’m not sure how intentional this was (see test verifying this)… The logic may have been that since null isn’t returned when reading, the property setter likely coalesces the given value to some non-null value (e.g. empty string). But the same logic can theoretically work on the read side as well (e.g. property getter can return null for empty string in the database).

We should consider changing to only mark properties as non-nullable if both read and write are non-nullable, requiring users to be explicit in other cases. This would be a breaking change, though I doubt lots of people are doing different read/write nullability on entity properties. Properties which are read-only or write-only would only need to be non-nullable on the supported operation, obviously.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
AndriySvyrydcommented, Dec 13, 2021

That’s true, but as the property is written above, users can assign null to the property just fine (no compiler warning) but will then get an exception when doing SaveChanges; that seems like a bit of a pit of failure.

I’d consider the above definition to be user error, so an exception is warranted. Also not making a breaking change is a plus.

1reaction
AndriySvyrydcommented, Jan 12, 2022

Since it is non-null on read then it will be persisted as non-null, so it makes sense to treat the column as required. This pattern could be useful when lazily cleaning nulls in store data. So we should keep current behavior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Non-nullable property must contain a non-null value when ...
The compiler is warning you that the default assignment of your string property (which is null) doesn't match its stated type (which is...
Read more >
Working with nullable reference types - EF Core
Exercise caution when enabling nullable reference types on an existing project: reference type properties which were previously configured as ...
Read more >
C# 8.0 nullable references and serialization
If a reference-typed property is naturally non-nullable, ... (And this works whether the relevant properties are read/write or read-only.).
Read more >
Nullable Reference types in C# – Best practices
While assigning a null value to a non-nullable value type causes a compiler error, doing the same with a non-nullable reference type will...
Read more >
Documentation - Utility Types
Constructs a type with all properties of Type set to readonly , meaning the properties of the constructed type cannot be reassigned. Example....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found