Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nullability for Fluent API #24457

Merged
merged 1 commit into from
Mar 20, 2021
Merged

Fix nullability for Fluent API #24457

merged 1 commit into from
Mar 20, 2021

Conversation

AndriySvyryd
Copy link
Member

No description provided.

@AndriySvyryd AndriySvyryd requested a review from roji March 19, 2021 22:46
@@ -79,7 +79,7 @@ public class CollectionNavigationBuilder<TEntity, TRelatedEntity> : CollectionNa
/// </param>
/// <returns> An object to further configure the relationship. </returns>
public virtual ReferenceCollectionBuilder<TEntity, TRelatedEntity> WithOne(
Expression<Func<TRelatedEntity, TEntity>>? navigationExpression)
Expression<Func<TRelatedEntity, TEntity?>>? navigationExpression)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am curious, what's the need for this (here and everywhere below)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To allow configuring nullable navigations (since TEntity is marked as non-nullable)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah of course.

One interesting question this raises... rather than changing the lambda's return type, we could change the generic constraint for TRelatedEntity to be class? instead of class. This would flow the nullability of the property to the returned ReferenceCollectionBuilder - which may be good, but also bad. For example:

modelBuilder.Entity<Blog>()
    .HasOne(b => b.Details)
    .WithOne(d => d.Blog);

If Blog.Details is nullable and HasOne above flows the nullability to ReferenceNavigationBuilder, then you get a warning in WithOne (this can be fixed by having WithOne accept a lambda with a nullable first type parameter as well).

I guess the question is... is there value in flowing the nullability of d.Details onwards? It feels slightly more correct, but I'm not sure if we actually need it or not.

Another unrelated idea - I think we discourage nullable collection navigations. Not sure to what extent this is supported/unsupported, but if we really want to discourage it we can disallow it via annotation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flowing the nullability means one would need to use a bang WithOne(d => d!.Blog);, also keeping the non-nullable constraint is more correct as it's applied to the entity type, not the navigation type.

We handle nullable collections fine, what we don't allow are nulls in the collection, that's what the signature still restricts

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants